Laravel Livewire Pagination Example

Hello Friends,
This post is based on laravel livewire pagination. This post will discuss how to create pagination in laravel livewire. This example is based on a simple laravel example.
I will show you how to implement pagination with laravel livewire.
Let's start.

Install Laravel

Our First step is to install laravel, for this, I am using the composer command to create a laravel project. To install laravel open your command line tool or terminal and type the below command.

composer create-project laravel/laravel livewire

In this command livewire is my project folder name.

Connecting Database

After installing the project we need to connect our project with the database. For this, we can use any database service. I am using MySQL for connecting the project with the database.
Just change the creds in the .env file.


Creating Dummy users for the paginations 

Once our database is connected. Then we are ready to create some dummy data. For this, we will use laravel tinker and factory to create some dummy data.
You can also check post related to tinker.
To create dummy records. Open your cmd or terminal and run the below code.

php artisan tinker

By running this command 100 records will be created in the database user table.

Installing Livewire

In this step, we will simply install laravel livewire In our laravel project. To install livewire run the following command.

composer require livewire/livewire

Create Livewire component

After installing laravel livewire, we need to create the component for the user paginations. To create paginate component run the following command.

php artisan make:livewire user-pagination

This command will create the following files.



Now, both files will be used in the pagination.
Let's update the following files to achieve our pagination component. 


namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\User;
class UserPagination extends Component
    use WithPagination;
     * Write code on Method
     * @return response()
    public function render()
        return view('livewire.user-pagination', [
            'users' => User::paginate(15),

Now, change the blade file for the pagination.

    <table class="table-auto" style="width: 100%;">
          <th class="px-4 py-2">ID</th>
          <th class="px-4 py-2">Name</th>
          <th class="px-4 py-2">Email</th>
          <th class="px-4 py-2">Created At</th>
        @foreach ($users as $user)
            <td class="border px-4 py-2">{{ $user->id }}</td>
            <td class="border px-4 py-2">{{ $user->name }}</td>
            <td class="border px-4 py-2">{{ $user->email }}</td>
            <td class="border px-4 py-2">{{ $user->created_at }}</td>
    {{ $users->links() }}

Create Route

Let's create a route for the pagination. So we can access the pagination page or ui.


Route::get('users', function () {
    return view('users');

Create Blade view file

Let's create a view file with users name to access the route file. After creating the file we need to add the following code. 

<!DOCTYPE html>
    <title>Laravel Livewire Pagination Example - Templatebench</title>
    <link rel="stylesheet" href="" integrity="sha512-wOgO+8E/LgrYRSPtvpNg8fY7vjzlqdsVZ34wYdGtpj/OyVdiw5ustbFnMuCb75X9YdHHsV5vY3eQq3wCE4s5+g==" crossorigin="anonymous" />
    <link rel="stylesheet" href="" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<div class="container">
    <div class="card">
      <div class="card-header">
        Laravel Livewire Pagination Example
      <div class="card-body">

Now our project is ready to run.

Start php server

To run our project run the following code.

php artisan serve.

You can check pagination at following.