gpt4 book ai didi

php - undefined variable : tasks (0) when trying to output every database(Tasks) entry associated to one main database entry (Projects)

转载 作者:行者123 更新时间:2023-11-29 15:55:35 24 4
gpt4 key购买 nike

我是 Laravel 新手,并尝试输出相关的每个任务 对于一个项目,当我注释掉该部分时一切正常 关于任务。

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

当我进入 php artisantinker 时,一切都显示得很好

>>> 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

最佳答案

您似乎没有将 $task 变量推送到您的 View 中。从你的 Controller 你可以做这样的事情:

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>

关于php - undefined variable : tasks (0) when trying to output every database(Tasks) entry associated to one main database entry (Projects),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56496052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com