我只是想从我的表名帖子中获取所有数据但是发生错误它不会让我在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
你应该用它作为
return view('posts.index')->with(compact('posts'));
你最好像这样使用with
:
return view('posts.index')->with('posts',$posts);
不要在$ posts中使用单引号
return view('posts.index')->withPosts($posts);
或尝试第二种方式
return view('posts.index', compact('posts'));
你可以使用compact
方法
return view('posts.index',compact('posts'));
如果其他解决方案无效,请尝试此操作。
我可能认为你在表单中错过了这一行“enctype =”multipart / form-data“。 见下面的例子:
<form action="{{ route('upload.files') }}" method="POST" enctype="multipart/form-data">
<input id="fileupload" type="file" name="attachment[]" multiple>
</form>
别忘了单击向上箭头。我希望我能帮助你:)