Laravel Pagination Tutorial

Hello dev, Today we are going to learn Laravel Pagination Tutorial. This tutorial will cover on how to make pagination in laravel application.

This is a short guide on pagination in laravel 9. Here you will learn laravel 9 pagination example blade. In this article, we will implement a laravel 9 pagination example code. This tutorial will give you simple example of laravel 9 pagination example. follow bellow step for create pagination in laravel 9 example.

We know pagination is a primary requirement of each and every project. so if you are beginner with laravel than you must know how to use pagination in laravel 9 and what is other function that can use with laravel 9 pagination.

In this example we will explain you from scratch how to working with laravel pagination. so let’s follow bellow tutorial for creating simple example of pagination with laravel 9.

Steps for Laravel Pagination Tutorial

  • Step 1: Add Route
  • Step 2: Create Controller
  • Step 3: Create Blade File
  • Step 4: Testing

Also Read: Laravel Carbon Time Format AM PM Example Code

Step 1: Add Route

First thing is we put one route in one for list users with pagination. So simple add both routes in your route file.

routes/web.php

Route::get('users', 'UserController@index');

Step 2: Create Controller

Same things as above for route, here we will add one new method for route. index() will return users with pagination data, so let’s add bellow:

app/Http/Controllers/UserController.php

<?php
   
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = User::paginate(10);
        return view('users',compact('data'));
    }
}

Also Read: Laravel Change Mail Driver Dynamically Example

Step 3: Create Blade File

In this step, you need to create users blade file and put bellow code with links() so it will generate pagination automatically. So let’s put it.

resources/views/users.blade.php

@extends($theme)
  
@section('content')
  
<table class="table table-bordered">
    <thead>
        <tr>
            <th>Name</th>
            <th width="300px;">Action</th>
        </tr>
    </thead>
    <tbody>
        @if(!empty($data) && $data->count())
            @foreach($data as $key => $value)
                <tr>
                    <td>{{ $value->name }}</td>
                    <td>
                        <button class="btn btn-danger">Delete</button>
                    </td>
                </tr>
            @endforeach
        @else
            <tr>
                <td colspan="10">There are no data.</td>
            </tr>
        @endif
    </tbody>
</table>
   
{!! $data->links() !!}
  
@endsection

Also Read: Laravel Mail Send with PDF Attachment Example

Now you can run and check this example. it is a very simple and basic example.

If you need advance used of pagination then you can see bellow how to use.

Pagination with appends parameter

{!! $data->appends(['sort' => 'votes'])->links() !!}

Pagination with appends request all parameters

{!! $data->appends(Request::all())->links() !!}

Also Read: How to set CC And BCC Email Address In Laravel Mail?

Step 4: Testing

Everything is done! now you can test the laravel application by running the following command in terminal

php artisan serve

and open the following link in any web browser.

http://127.0.0.1:8000/users

Conclusion

Today, We had learn Laravel Pagination Tutorial. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: How to Check Running Laravel App Environment?

1 Comment

Leave a Reply