【发布时间】:2017-11-26 16:11:12
【问题描述】:
我只是想从我的表名帖子中获取所有数据,但发生错误,它不会让我在 index.blade.php 上显示数据
public function index()
{
//
$posts = Post::all();
return view('posts.index')->withPosts('$posts');
}
这是我的 index.blade.php
@foreach( $posts as $post )
<div class="post-preview">
<a href="post.html">
<h2 class="post-title">
{{ $post->title }} -> this is the one i want to display
</h2>
<h3 class="post-subtitle">
{{ $post->body }}
</h3>
</a>
<p class="post-meta">Posted by <a href="#">Start Bootstrap</a> on September 24, 2014</p>
<button type="button" class="btn btn-secondary"> Edit </button>
<button type="button" class="btn btn-secondary">Delete</button>
</div>
<hr>
@endforeach
【问题讨论】:
-
dd($posts) 在你的控制器中看看有什么输出?
-
并尝试以
return view('posts.index')->with(compact('posts'));返回 -
@blue moon 嘿,您的代码有效,感谢您的帮助
-
@blue moon 怎么办?