Laravel 8 未定义的变量帖子

我有一个错误..它说:


未定义的变量: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 &rarr;</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');

    }



繁华开满天机
浏览 125回答 1
1回答

鸿蒙传说

在索引方法中,您需要传递postspublic function index(){&nbsp; &nbsp; $posts = Post::all();&nbsp; &nbsp; return view('admin.posts.index',compact('posts'));}你没有传递posts variable刀片文件。因此,它给了你undefined error。
打开App,查看更多内容
随时随地看视频慕课网APP