我正在尝试从所有用户获取帖子,以及仅来自当前用户的任务。全部通过单一功能和路线传递到单个页面。它会返回错误页面。
调节器
public function getDashboard()
{
$user = Auth::user();
$userId = $user->id;
$posts = Post::orderBy('created_at', 'desc')->get();
$tasks = Task::where('employee_id', $userId )->get();
return view('dashboard', compact('posts', 'tasks'));
}
路线
Route::get('/dashboard', [
'uses' => 'PostController@getDashboard',
'as' => 'dashboard',
])->middleware('auth');
刀片/查看
<div>
@foreach($tasks as $task)
<p data-taskId="{{ $task->id }}">{{ $task->task_body }}</p>
@endforeach
</div>
看起来可能是语法问题,因为紧凑应该可以正常工作。在你的控制器中尝试这个:
return view('dashboard', compact('posts', 'tasks'));
然后在你的视图中,确保使用变量而不是类名,正如Karl Hill所说,它在()
中使用,而不是{{}}
:
@foreach($posts as $post)
{{$post->nameOrWhatever}}
@endforeach
@foreach($tasks as $task)
{{$task->nameOrWhatever}}
@endforeach