Livewire provides a WithPagination
trait that makes it easy to add pagination to your component.
Run the following command to create a Livewire component:
php artisan make:livewire ProductList
$products,
]);
}
}
WithPagination
: Enables pagination in the component.
Product::paginate(10)
: Fetches 10 products per page.
Name
Price
Category
@foreach ($products as $product)
{{ $product->name }}
${{ $product->price }}
{{ $product->category }}
@endforeach
{{ $products->links() }}
@foreach
: Loops through paginated products and displays them in a table.
$products->links()
: Generates pagination controls (e.g., next, previous, and page numbers).
Laravel’s paginator uses Tailwind CSS by default. To customize the styles, you can update the pagination view.
Run the following command to publish Laravel’s pagination views:
php artisan vendor:publish --tag=laravel-pagination
This will create a resources/views/vendor/pagination
directory with customizable pagination templates.
Edit the default.blade.php
file to apply your custom styles.
You can combine pagination with search and filtering to make your components more dynamic.
Update ProductList.php
:
public $search = '';
public function updatingSearch()
{
$this->resetPage();
}
public function render()
{
$products = Product::where('name', 'like', '%' . $this->search . '%')
->paginate(10);
return view('livewire.product-list', [
'products' => $products,
]);
}
Update product-list.blade.php
:
Name
Price
Category
@foreach ($products as $product)
{{ $product->name }}
${{ $product->price }}
{{ $product->category }}
@endforeach
{{ $products->links() }}
wire:model.debounce.300ms="search"
: Binds the search input to the search
property with a 300ms delay.
updatingSearch()
: Resets pagination to the first page when the search query changes.
For modern applications, you can replace traditional pagination with infinite scrolling.
Install the livewire-ui
package for infinite scrolling:
composer require livewire-ui/livewire-ui
Update ProductList.php
:
use LivewireUI\InfiniteScroll\WithInfiniteScroll;
class ProductList extends Component
{
use WithInfiniteScroll;
public function render()
{
return view('livewire.product-list', [
'products' => Product::paginate(10),
]);
}
}
Update product-list.blade.php
:
@foreach ($products as $product)
{{ $product->name }}
@endforeach
By implementing these pagination techniques, your Livewire component will:
Efficiently handle large datasets with pagination controls.
Provide dynamic updates with search and filter capabilities.
Enhance user experience with infinite scrolling.
Adding pagination to Livewire components with WithPagination
.
Customizing pagination controls and views.
Combining pagination with search and filters.
Implementing infinite scrolling for modern applications.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.