vendredi 20 mars 2020

ErrorException Undefined variable: posts

I want to get some field data in database in several different tables and i have made relationship between tables in post model , and in ListingPageController i have assigned post model and created variable named $posts to use it in list.blade.php page while i am trying to use it will give me this error please help

Following code is ListingPageController page:

<?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'));
  }


}

Below code is listing.blade.php page

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

<div class="entity_wrapper">
    <div class="entity_title header_purple">
        <h1><a href="/"></a></h1>
    </div>
    <!-- entity_title -->

    <div class="entity_thumb">
        <img class="img-responsive" src="/ " alt="">
    </div>
    <!-- entity_thumb -->

    <div class="entity_title">
        <a href="/" target="_blank"><h3>  </h3></a>
    </div>
    <!-- entity_title -->

    <div class="entity_meta">
        <a href="#"></a> , by: <a href="/"> </a>
    </div>
    <!-- entity_meta -->

    <div class="entity_content">
        
    </div>
    <!-- entity_content -->

    <div class="entity_social">

        <span><i class="fa fa-comments-o"></i> <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="/" alt="">
            </div>
            <!-- top_article_img -->

            <div class="category_article_title">
                <h5>
                    <a href="/" target="_blank">  </a>
                </h5>
            </div>
            <!-- category_article_title -->

            <div class="article_date">
                <a href="#"></a> , by: <a href="/"></a>
            </div>
            <!-- article_date -->

            <div class="category_article_content">
               
            </div>
            <!-- category_article_content -->

            <div class="article_social">

                <span><i class="fa fa-comments-o"></i> <a href="#"> Comments</a></span>
            </div>
            <!-- article_social -->

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

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

    </div>
      @endif
        @endif
         @endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire