未定义 variable:forums

Undefined variable:forums

未定义变量报错我该怎么办? 论坛管理员:

public function index()
{
    $forums = Forum::all();
    return view('forum.index') -> withForum($forums);
}

路线:

Route::resource('/forum','ForumController');

welcome.blade :

@foreach($forums as $forum)
 <div class="col-md-7">{{ $forum->title }}</div> 
     <div class="col-md-5 text-center">
     <span class="label label-warning">پاسخ : ۱۰</span>
     <span class="label label-primary">تشکر : ۱۰</span>
     <span class="label label-success">RaymondDragon</span>
 </div>@endforeach

错误异常:

Undefined variable: forums (View: /opt/lampp/htdocs/forums/resources/views/welcome.blade.php)

我认为您需要像这样定义论坛变量:

return view('forum.index')->with('forums', $forums);
return view('forum.index', compact('forums'));

是实现目标的好方法。

return view('forum.index') -> withForums($forums);
// or
return view('forum.index') -> with('forums', $forums);
return view('forum.index', ['forums' => $forums]);