ErrorException 未定义的变量:帖子

ErrorException Undefined variable: posts

我想在数据库中的几个不同的表中获取一些字段数据,我在 post 模型中建立了表之间的关系,在 ListingPageController 中我分配了 post 模型并创建了名为 $ 的变量posts 在 list.blade.php 页面中使用它,而我正在尝试使用它会给我这个错误,请帮助

以下代码为ListingPageController页面:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class ListingPageController extends Controller
{
    public function index(){
        return view('front.listing');
    }

    public function listing($id){ 

         $posts = Post::with(['comments','category','creator'])
                      ->where('status',1)
                      ->where('category_id',$id)
                      ->orWhere('created_by',$id)
                      ->orderBy('id','DESC')->paginate(3); 

       return view('front.listing',compact('posts'));
  }


}

下面的代码是listing.blade.php页面

@foreach($posts as $key=>$post)
                @if($key === 0)

<div class="entity_wrapper">
    <div class="entity_title header_purple">
        <h1><a href="{{ url('/category') }}/{{ $post->category_id }}">{{ $post->category->name }}</a></h1>
    </div>
    <!-- entity_title -->

    <div class="entity_thumb">
        <img class="img-responsive" src="{{ asset('post') }}/{{ $post->main_image }} " alt="{{ $post->title }}">
    </div>
    <!-- entity_thumb -->

    <div class="entity_title">
        <a href="{{ url('/details') }}/{{ $post->slug }}" target="_blank"><h3> {{ $post->title }} </h3></a>
    </div>
    <!-- entity_title -->

    <div class="entity_meta">
        <a href="#">{{ date('F j- Y',strtotime($post->created_at)) }}</a> , by: <a href="{{ url('/author') }}/{{ $post->creator->id }}">{{ $post->creator->name }} </a>
    </div>
    <!-- entity_meta -->

    <div class="entity_content">
        {{ str_limit( $post->short_description,200,'...' )  }}
    </div>
    <!-- entity_content -->

    <div class="entity_social">

        <span><i class="fa fa-comments-o"></i>{{ count($post->comments) }} <a href="#"> Comments</a></span>
    </div>
    <!-- entity_social -->

</div>
<!-- entity_wrapper -->
      @else
    @if($key === 1)
<div class="row">
    @endif <!-- first if will be ended here -->
    <div class="col-md-6" style="min-height: 555px;margin-bottom:2%">
        <div class="category_article_body">
            <div class="top_article_img">
                <img class="img-fluid" src="{{ asset('post') }}/{{ $post->list_image }}" alt="{{ $post->title }}">
            </div>
            <!-- top_article_img -->

            <div class="category_article_title">
                <h5>
                    <a href="{{ url('/details') }}/{{ $post->slug }}" target="_blank"> {{ $post->title }} </a>
                </h5>
            </div>
            <!-- category_article_title -->

            <div class="article_date">
                <a href="#">{{ date('F j- Y',strtotime($post->created_at)) }}</a> , by: <a href="{{ url('/author') }}/{{ $post->creator->id }}">{{ $post->creator->name }}</a>
            </div>
            <!-- article_date -->

            <div class="category_article_content">
               {{ str_limit( $post->short_description,100,'...' )  }}
            </div>
            <!-- category_article_content -->

            <div class="article_social">

                <span><i class="fa fa-comments-o"></i>{{ count($post->comments) }} <a href="#"> Comments</a></span>
            </div>
            <!-- article_social -->

         </div>
        <!-- category_article_body -->

        </div>
    <!-- col-md-6 -->
    @if($loop->last)

    </div>
      @endif
        @endif
         @endforeach

索引函数没有定义 posts 变量。您需要更新您的视图或传递一个空变量 posts:

public function index(){
    return view('front.listing', ['posts'=>[]]);
}

或者在您看来:

@if(isset($posts))
    @foreach($posts as $key=>$post)
    ...
@endif

或者创建新视图,更改逻辑等,由您决定。

你能试试看它告诉你什么吗?

return view('front.listing', ['posts' => $posts]);

EDIT1

验证您传递给 listing.blade.php 的内容是否正确.. 在 listing.blade.php 的第一行键入 {{ dd($posts) }}

并检查您得到的结果。