ErrorException未定义变量:帖子

问题描述 投票:0回答:1

我想在几个不同的表中获取数据库中的一些字段数据,并且在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
php laravel laravel-5 laravel-5.8
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.