Hello, laravel web developers! This article will show how to add pagination in laravel 11 Livewire. Here, we'll learn to add without page refresh laravel 11 livewire pagination. Livewire offers the ability to paginate results within a component.
In Laravel Livewire, you can use the paginate() method to add pagination. Also, you can reset after the filtering data using the resetPage() function.
Additionally, you can add multiple paginations on the same page and also customize them as per the requirements.
How to add Pagination in Laravel 11 Livewire
In this step, we'll install the laravel 11 application using the following command.
composer create-project laravel/laravel example-app
Then, we'll create some dummy records using the following command.
php artisan tinker
User::factory()->count(100)->create()
Next, we'll install Laravel Livewire using the following command.
composer require livewire/livewire
Then, we'll create a pagination component using the following command.
php artisan make:livewire user-pagination
This command will generate two files as per the below path
app/Http/UserPagination.php
resources/views/livewire/user-pagination.blade.php
app/Livewire/UserPagination.php
<?php
namespace App\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(10),
]);
}
}
resources/views/livewire/user-pagination.blade.php
<div>
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $users->links('pagination::bootstrap-5') }}
</div>
Then, we'll define the routes in the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('user-pagination', function () {
return view('default');
});
Next, we'll create a blade file and use @livewireStyles, @livewireScripts and @livewire('contact-form').
resources/views/default.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to add Pagination in Laravel 11 Livewire - Websolutionstuff</title>
@livewireStyles
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3">How to add Pagination in Laravel 11 Livewire - Websolutionstuff</h3>
<div class="card-body">
@livewire('user-pagination')
</div>
</div>
</div>
</body>
@livewireScripts
</html>
Now, run the laravel 11 application using the following command.
php artisan serve
You might also like:
Hello, laravel web developers! In this article, we'll see how to file upload in laravel 11 Livewire. Here, we'll...
Jun-10-2024
Hey there, I recently found myself in a situation where I needed to downgrade my PHP version from 8.2 to 8.1 on my Ubunt...
Nov-01-2023
In this post we will see Laravel 8 qr code generate example. we will generate QR Code using simple-qrcode package....
Jun-30-2021
Hey there! If you're diving into the world of PHP development on your Ubuntu 22.04 machine, you'll likely come a...
Jan-10-2024