ChrisPercival's avatar

Problem with pagination

Hi All

I have a problem with pagination & record sets.

When all records are listed it works fine, however when I get a record set back and click on the next or any page. The set seems to be lost and all records are shown again.

Heres the code in the controller

public function index(Request $request)
{     
    $listings = Listing::where('published', '=', 2)
        ->orderBy('title', 'asc')
        ->paginate(20);         
    return view('listings.index', compact('listings'));
}

And the code on the page

        @foreach ($listings as $listing)
            @include ('listings.index_item')
            <hr>
        @endforeach
        <div> Showing {{($listings->currentpage()-1)*$listings->perpage()+1}} to {{$listings->currentpage()*$listings->perpage()}} of  {{ number_format($listings->total())}} entries</div>
        {{ $listings->links() }} 

Any ideas

Thanks Chris

0 likes
1 reply
janakaontomatrix's avatar

Try:

{{ $listings->links() }} instead of:

<div> Showing {{($listings->currentpage()-1)*$listings->perpage()+1}} to {{$listings->currentpage()*$listings->perpage()}} of  {{ number_format($listings->total())}} entries</div>
        {{ $listings->links() }} 

to check if your calculations alter the $listings object. If {{ $listings->links() }} alone works then you have to assign the $listings to a new variable and calculate.

Please or to participate in this conversation.