Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pagination in laravel 5?

am trying to port my laravel4 application to laravel 5 . In the previous version i could use the following method for generating pagination urls .

In controller:

$this->data['pages']= Page::whereIn('area_id', $suburbs)->where('score','>','0')->orderBy('score','desc')->paginate(12); 

and after sharing the data array with the view, i could user

In views :

{{$pages->links()}} 

In laravel 5 the doing that results in following error

ErrorException in AbstractPaginator.php line 445: call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Support\Collection' does not have a method 'links' 

not sure what i am missing here, can somebody help ?

like image 953
Sojan Jose Avatar asked Jan 16 '15 09:01

Sojan Jose


People also ask

How can we manually create pagination in Laravel?

Just to say (Laravel doc): When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator. Could you explain parameters please? The parameters are discussed over the construct method: laravel.com/api/5.0/Illuminate/Pagination/…

How can I get pagination page numbers in Laravel?

In your case you can use $articles->links() in your view to generate the pagination navigation buttons. But if you want to manually set the page then you can do this. $articles = Articles::paginate(5, ['*'], 'page', $pageNumber); The default paginate method takes the following parameters.

How can I use pagination in Laravel 8?

To perform pagination in laravel, you just have to use the paginate () function instead of all () or get() functions as used in maximum time. Paginate method takes a number as a parameter. It defines how much data you want to show on one page or section.


2 Answers

In Laravel 5 there is no method "links" you can try this

{!! $pages->render() !!} 
like image 119
Alasu Paul Sabrin Avatar answered Oct 02 '22 07:10

Alasu Paul Sabrin


In other frameworks, pagination can be very painful. Laravel 5 makes it a breeze. For using it, first you have to make a change in your controller code where you calling data from the database:

 public function index() {                $users = DB::table('books')->simplePaginate(5);         //using pagination method         return view('index', ['users' => $users]); } 

...after that you can use this code:

<?php echo $users->render(); ?> 

That will make you use simple Laravel 5 beauty.

like image 29
David Avatar answered Oct 02 '22 05:10

David