Pagination in Laravel

Sometimes your website fetching data from database like hundreds or thousands data list. It will make your page slow and may cause to reduce your site traffic.

Pagination is very complicated feature through php but laravel makes it very prosperous. Its generate intelligent "range" of links based on the current page. The generated HTML is compatible with the Bootstrap CSS framework.

App/Http/routes.php

In this route file we are trying to set pagination for users where 10 limit has been set for per page.

<?php
Route::get('user', function() {
   // $users have 10 records from users table
    $users = App\User::paginate(10);
    return view('users', compact('users'));
});

resources/views/users.blade.php

Creating pagination links using the render method

<?php
<h1>Our User List</h1>
@foreach($users as $user)
    <li>{!! link_to( "user/{$user->name}", $user->name ) !!}</li>
@endforeach

{!! $users->render() !!} 

 

Something to say? Tell us in comment section.