Paginate Issue Laravel 5.6

554 Views Asked by At

Was wondering if someone could check this out and let me know what I'm doing wrong. I'm trying to use paginate on a database query and render it in my view.

Is my DB query incorrect? I was following the docs to create it. Also read that I needed to remove ->get() when using paginate.

This is what is giving the error on my view: {{$item->paginate(4)}}, same happens if I use {{$item->links(4)}}

Everything renders fine if I remove paginate from the query in the controller..?

Here is what I'm working with.

Controller:

        public function index()
{
   // $news = DB::table('news')->orderBy('id', 'DESC')->paginate(4);
    $news = DB::table('news')->select('id','title','description','listing_image','created_at','updated_at')->orderBy('id', 'DESC')->paginate(4);
    return view('news.index',compact('news'));
}

View: (Getting error: Call to undefined method stdClass::paginate())

@if ($news->count())
@foreach($news as $item)
...html
 @if ($item->listing_image)
...more html

    @else
    @endif 

... more html

@endforeach

{{$item->paginate(4)}}

@endif 
1

There are 1 best solutions below

0
On BEST ANSWER

change {{$item->paginate(4)}} to {{ $news->links() }}