Php Laravel compact不起作用
我正在尝试建立一个发布系统,在帖子上有一个资源路由组合。当我尝试运行应用程序查看帖子时,它返回一个错误,指出在视图中找不到帖子。我有索引和显示功能的控制器代码:Php Laravel compact不起作用,php,laravel,view,Php,Laravel,View,我正在尝试建立一个发布系统,在帖子上有一个资源路由组合。当我尝试运行应用程序查看帖子时,它返回一个错误,指出在视图中找不到帖子。我有索引和显示功能的控制器代码: public function index() { $posts = Post::latest()->get(); return view('view', compact('posts')); } public function show(Post $post) { return view('posts
public function index()
{
$posts = Post::latest()->get();
return view('view', compact('posts'));
}
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
我的应用程序视图使用post变量来显示帖子:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-2">
<div class="panel panel-default">
<!-- Posts will be displayed on the same panel -->
<div class="panel-body" id="view">
@foreach($posts as $post)
<article id="post">
<a href="/view/posts{{ $post->id }}">
{{ $post->title }}
</a>
<div class="body">
{{ $post->body }}
</div>
<!-- Footer for posts will include interaction features -->
</article>
@endforeach
</div>
</div>
</div>
</div>
</div>
@foreach($posts作为$post)
{{$post->body}
@endforeach
是否存在laravel安装不正确的情况?紧凑型功能设置是否正确 索引操作的返回错误
return view('view', compact('posts'));
用'posts'更改'view',控制器索引函数应如下所示:
public function index()
{
$posts = Post::get();
return view('posts.index', compact('posts'));
}
使用compact(‘posts’)
如果您是初学者,请查看laracasts视频系列,以更好地了解Laravel框架。尝试
返回$posts
要查看它是否实际返回任何值,如果它没有请共享latest()
方法,您的错误可能是因为您的代码已更正您的问题令人困惑,您有2个返回视图,但仅显示1页。同样作为@hazelcodes,确保您得到了结果dd($posts)
change view with posts是什么意思?“view”是我想将帖子发送到的视图刀片,按从早到晚的顺序排列。