2017-12-15 3 views
0

정의되지 않은 변수가 오류를 발생시킵니다. 어떻게해야합니까? ForumController :정의되지 않은 변수 : 포럼

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 

ErrorException :

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

당신이 – lagbox

+0

너무 감사'welcome'보기를 반환하지 않습니다 당신이 여기 가지고 ... 잘못된 경로 및 컨트롤러를 보이고있다 ... 나는 노력 할게요 그리고 나는를 발표합니다 결과. – RaymondDragon

+0

당신의'forum.index' 뷰는 'includes'를 포함 할 수 있습니까? – lagbox

답변

2

난 당신이 생 같은 변수 포럼을 정의 할 필요가 있다고 생각 S :

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

시도했지만 더 많은 트랙 오류가있었습니다 ./opt/lampp/htdocs/forums/resources/views/welcome.blade.php – RaymondDragon

0
return view('forum.index', compact('forums')); 

은 당신의 목표를 달성하는 깔끔한 방법입니다.

+0

답변 해 주셔서 감사합니다 ... 불행히도 작동하지 않았습니다. 위의 오류에 유의하십시오. – RaymondDragon

+0

forum.index는 welcome.blade라고? –

+0

예 ... 내가 말했듯이. – RaymondDragon

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

이제 오류없이 $ forums를 사용할 수 있습니다. – HiKangg