php Laravel 中的“使用未定义常量...”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22274943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:47:03  来源:igfitidea点击:

"Use of undefined constant..." error in Laravel

phptemplateslaravel

提问by user1801060

I am a Laravel newbie. I want to pass the results of a database query to a view. I get an error message "Use of undefined constant tasks - assumed 'tasks'". What am I doing wrong?

我是 Laravel 新手。我想将数据库查询的结果传递给视图。我收到一条错误消息“使用未定义的常量任务 - 假定为‘任务’”。我究竟做错了什么?

My code is as follows:

我的代码如下:

class TasksController extends BaseController{
    public function index(){
        $tasks = Task::all();

        //return View::make(tasks.index, ['tasks' => $tasks]);
        return View::make(tasks.index, compact('tasks'));
    }

A snippet from my template page is shown below:

我的模板页面中的一个片段如下所示:

<body>
    <h1>All tasks!</h1>

    @foreach($tasks as $task)
        <li>{{ $task-title }} </li>
    @endforeach

回答by nCore

return View::make('tasks.index')->with(compact('tasks'));

also change:

也改变:

<li>{{ $task-title }} </li>

to

<li>{{ $task->title }} </li>

should be like this.

应该是这样的。

回答by Krish R

Try this,

尝试这个,

 return View::make(tasks.index, $tasks);

instead of

代替

return View::make(tasks.index, compact('tasks'));