gaan10's avatar

Pagination for the view is not working in laravel5.3

I am trying to use pagination for my view pages but getting error as : Call to a member function links() on array

Limiting to display records using paginate in controller is fine,but issue is when passing to the view. Controller code is:

$referredProcedureBookings = Gateway::procedurebooking()->getReferredProcedureBooking()->paginate(5);
            $new_records = [];
            foreach ($referredProcedureBookings as $referredProcedureBooking) 
            {
                $records['ProcedureId'] = $referredProcedureBooking->id;
                $records['ProcedureName'] = $referredProcedureBooking->procedure_name;
            $new_records[] = $records;    
            }
            return view('procedurebookings',compact('new_records');
In my view its like this:
@foreach($new_records as $new_recordss)
<td><div class="col-md-2"><h5 class="panel-body">{{ $new_recordss['Name'] }}</h5></div></td>
      
<td><div class="col-md-2 col-md-pull-6"><h5 class="panel-body">{{ $new_recordss['Age'] }}</h5></div></td>
@endforeach 
{{ $new_records->links() }}

error i am getting is :Call to a member function links() on array

Even if i use :{{ $new_records[links()] }}

then i get :Call to undefined function links()

how should i use the links method?

0 likes
2 replies
gaan10's avatar
gaan10
OP
Best Answer
Level 1

I think i should pass referredProcedureBookings to links. {{ referredProcedureBookings->links() }}

1 like
ts's avatar

Yeah, as above. The paginate method doesn't return an array, but an instance of Illuminate\Pagination\LengthAwarePaginator. By iterating through this and adding the values to an array, you're losing all the methods that the LengthAwarePaginator provides. You should write it like so:

$referredProcedureBookings = Gateway::procedurebooking()->getReferredProcedureBooking()->paginate(5);
return view('procedurebookings', compact('referredProcedureBookings');

There's more info in the documentation: https://laravel.com/api/5.3/Illuminate/Contracts/Pagination/LengthAwarePaginator.html

2 likes

Please or to participate in this conversation.