2

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="{{ 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
Turan Zamanlı
  • 3,828
  • 1
  • 15
  • 23
Muhamad
  • 247
  • 1
  • 10

2 Answers2

2

The index function doesn't have a posts variable defined. You need to update your view or pass an empty variable posts:

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

Or in your view:

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

Or create a new view, change the logic, etc, it's up to you.

Felippe Duarte
  • 14,901
  • 2
  • 25
  • 29
-1

Can you try it like that to see what it tells you?

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

EDIT1

Verify that the content you are passing to listing.blade.php is correct .. on the first line of listing.blade.php type {{ dd($posts) }}

and check what result you are getting.

leo95batista
  • 699
  • 9
  • 27