我想在我的表單下的主頁上顯示我的帖子。 我已經可以將帖子添加到我的數據庫中。LARAVEL - 將變量從控制器傳遞到查看
現在我只是得到未定義的變量,如果我嘗試在我的查看頁面中使用此變量。
路線=
Route::resource('posts', 'PostController');
控制器=
public function show()
{
$posts = Post::orderBy('created_at', 'asc')->get();
return view('/home', [
'posts' => $posts
]);
}
查看
@if (count($posts) > 0)
<div class="panel panel-default">
<div class="panel-heading">
Posts
</div>
<div class="panel-body">
<table class="table table-striped task-table">
<!-- Table Headings -->
<thead>
<th>Posts</th>
<th> </th>
</thead>
<!-- Table Body -->
<tbody>
@foreach ($posts as $post)
<tr>
<!-- Post Name -->
<td class="table-text">
<div>{{ $post->description }}</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endif
嘗試在控制器中打印$ posts以查看它顯示的內容。同樣在你的視圖中,在循環之前添加if條件if(isset($ posts))。並且,如果您需要更多幫助,請在此處發佈您的視圖代碼 – manian
顯示您的視圖代碼以訪問該變量。 –
你從哪裏得到一個錯誤?在網頁上?在日誌文件中? – PKeidel