<?php use Livewire\Component;use Livewire\WithPagination; new class extends Component { use WithPagination; public function render() { return view('livewire.users', [ 'users' => User::paginate(10), ]); }};
2. In Your Blade View
<div> {{-- Your content --}} @foreach($users as $user) <div>{{ $user->name }}</div> @endforeach {{-- Pagination --}} <x-ui.pagination :paginator="$users" /></div>
Advanced Usage
Custom Per Page
public int $perPage = 15; public function render(){ return view('livewire.products', [ 'products' => Product::paginate($this->perPage), ]);}
With Filtering
use Livewire\WithPagination; public string $search = ''; public function updatingSearch(){ $this->resetPage(); // Reset to page 1 when search changes} public function render(){ return view('livewire.items', [ 'items' => Item::query() ->when($this->search, fn($query) => $query->where('name', 'like', '%' . $this->search . '%') ) ->paginate(10), ]);}
Using LengthAwarePaginator
use Illuminate\Pagination\LengthAwarePaginator; public function getDataProperty(){ $allItems = collect([/* */]); $perPage = 10; $currentPage = $this->getPage(); $items = $allItems->forPage($currentPage, $perPage); return new LengthAwarePaginator( $items, $allItems->count(), $perPage, $currentPage, ['path' => request()->url()] );}
Pagination Logic
The component automatically handles:
- 7 or fewer pages: Shows all page numbers
- Current page near start:
[1, 2, 3, 4, ..., Last] - Current page near end:
[1, ..., N-3, N-2, N-1, N] - Current page in middle:
[1, ..., N-1, N, N+1, ..., Last]
Props
| Prop | Type | Required | Description |
|---|---|---|---|
paginator |
LengthAwarePaginator |
Yes | Laravel paginator instance |
Styling
The component uses Tailwind CSS with these key classes:
border-border: Border colorsbg-primary: Active page backgroundtext-primary-foreground: Active page texthover:bg-accent: Hover statedisabled:opacity-50: Disabled state
ARIA Attributes
role="navigation": Navigation landmarkaria-label="Pagination Navigation": Descriptive labelaria-current="page": Marks current pagedisabled: Disables prev/next when appropriate
Example: Cameroon Cities
$cities = City::where('country', 'Cameroon') ->orderBy('population', 'desc') ->paginate(5);
<table> <thead> <tr> <th>Ville</th> <th>Région</th> <th>Population</th> </tr> </thead> <tbody> @foreach($cities as $city) <tr> <td>{{ $city->name }}</td> <td>{{ $city->region }}</td> <td>{{ $city->population }}</td> </tr> @endforeach </tbody></table> <x-ui.pagination :paginator="$cities" />
Testing
The component includes data-test attributes:
data-test="pagination-container": Main containerdata-test="pagination-info": Results counterdata-test="pagination-previous": Previous buttondata-test="pagination-next": Next buttondata-test="pagination-pages": Page numbers containerdata-test="pagination-page": Individual page buttondata-test="pagination-ellipsis": Ellipsis indicator
Notes
- Uses
wire:clickfor Livewire navigation - No full page reloads
- Preserves query parameters
- Mobile responsive design
- Smooth transitions on state changes