我有一个错误..它说:未定义的变量:posts(查看:C:\xampp\htdocs\dinsospermasdes\bansus\resources\views\admin\posts\index.blade.php)它在 home.blade.php 中的工作方式如下:@foreach ($posts as $post) <div class="card mb-4"> <img class="card-img-top" src="{{filter_var($post->post_image, FILTER_VALIDATE_URL) ? $post->post_image : '/storage/' . $post->post_image}}" alt="Card image cap"> <div class="card-body"> <h2 class="card-title">{{$post->title}}</h2> <p class="card-text">{{Str::limit($post->body, '50', '....')}}</p> <a href="{{route('post', $post->id)}}" class="btn btn-primary">Read More →</a> </div> <div class="card-footer text-muted"> Posted on {{$post->created_at->diffForHumans()}} by <a href="#">{{$post->name}}</a> </div> </div> @endforeach但在 admin/posts/index.blade.php 中不起作用,如下所示: @foreach($posts as $post) <tr> <td>{{$post->id}}</td> <td>{{$post->title}}</td> <td> <img height="40px" src="{{filter_var($post->post_image, FILTER_VALIDATE_URL) ? $post->post_image : '/storage/' . $post->post_image}}"> </td> <td>{{$post->created_at->diffForHumans()}}</td> <td>{{$post->updated_at->diffForHumans()}}</td> </tr> @endforeach这是 PostController.php:class PostController extends Controller{ // public function index(){ $post = Post::all(); return view('admin.posts.index'); } public function show(Post $post){ return view ('blog-post', ['post'=>$post]); } public function create(){ return view ('admin.posts.create'); }
1 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
在索引方法中,您需要传递posts
public function index(){
$posts = Post::all();
return view('admin.posts.index',compact('posts'));
}
你没有传递posts variable刀片文件。因此,它给了你undefined error。
- 1 回答
- 0 关注
- 114 浏览
添加回答
举报
0/150
提交
取消