Pagination in Laravel

Hello everyone, welcome to justlaravel.com. Here we ‘ll see, how easy is pagination in laravel, in the previous post we saw about routing in laravel, here pagination is much more simpler than routing.
Working Demo Project on Github
In the last post, we just showed all the data from the database, here we ‘ll paginate that data by limiting 5 results per page.
We ‘ll take some code from the last post and edit it here, please check the previous post for reference,
Routing
Route for our controller will be the same as in the previous post,
Route::get ( '/callControllerDb', 'IndexController@getFromDb');
Controller logic
Controller logic will be slightly modified, we call paginate function here,
public function getFromDb() { $users = User::paginate(5); return view('sample')->withDetails($users); }
View for rendering pagination
In view file, sample.blade.php we render the paginate function which we used in the controller,
{!! $details->render() !!}
the above line of code renders the pagination, we can place that line in sample.blade.php
where ever we want our results to be paginated.
Complete sample.blade.php
file will be as follows,
<!DOCTYPE html> <html lang="en"> <head> <title>justLaravel - Routing in Laravel</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> </head> <body> <div class="fluid-container"> <h2>Sample User details</h2> <table class="table table-striped"> <thead> <tr> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> @foreach($details as $user) <tr> <td>{{$user->name}}</td> <td>{{$user->email}}</td> </tr> @endforeach </tbody> </table> </div> {!! $details->render() !!} </body> </html>
Working Demo Project on Github
See the below screenshots for the output,


This is how Laravel makes paginating data a breeze. Feel free to check previous posts on routing in Laravel, database setup in laravel.
Related posts:
Search functionality in Laravel: https://justlaravel.com/search-functionality-laravel/
Search functionality with Pagination: https://justlaravel.com/paginated-data-search-laravel/