未定义的变量:尝试输出与一个主数据库条目(项目)相关联的每个数据库(任务)条目时的任务(0)

时间:2019-06-07 14:17:20

标签: php mysql laravel

我是laravel的新手,正在尝试输出与之相关的每个任务     在一个项目中,当我注释掉零件时,一切正常     关于任务。

The problematic part of the view(show.blade.php):
<div>
  @foreach($project->$tasks as $task)
      <li>{{$task->description}}</li>
  @endforeach
</div>

当我进入php artisan tinker时,一切都很好

>>> App\Project::first()->tasks;
=> Illuminate\Database\Eloquent\Collection {#892
 all: [
   App\Task {#3200
     id: 1,
     project_id: 1,
     description: "Buy a map",
     completed: 0,
     created_at: null,
     updated_at: null,
   },
   App\Task {#3201
     id: 2,
     project_id: 1,
     description: "Inform friends",
     completed: 1,
     created_at: null,
     updated_at: null,
   },
 ],
}


The exact error message i get when trying to load the view:

ErrorException (E_ERROR)
Undefined variable: tasks 

1 个答案:

答案 0 :(得分:0)

您似乎没有将$ task变量推送到视图。在您的控制器上,您可以执行以下操作:

public function Showtask()
{
 $data['project']=DB::table('your-table')->get();

 return view('show',$data);

}

您的视图

<div>
  @foreach($project as $task)
      <li>{{$task->description}}</li>
  @endforeach
</div>