Pagination
Livewire offers the ability to paginate results within a component. This feature hooks into Laravel's native pagination features, so it should feel like an invisible feature to you.
Paginating Data
Let's say you have a show-posts
component, but you want to limit the results to 10 posts per page.
You can paginate the results by using the WithPagination
trait provided by Livewire.
use Livewire\Component;
use Livewire\WithPagination;
class ShowPosts extends Component
{
use WithPagination;
public function render()
{
return view('livewire.show-posts', [
'posts' => Post::paginate(10),
]);
}
}
<div>
@foreach ($posts as $post)
...
@endforeach
{{ $posts->links() }}
</div>
Now there will be rendered HTML links for the different pages at the bottom of your posts, and the results will be paginated.
Using A Custom Pagination View
Livewire provides 2 ways to customize the pagination links Blade view, rendered when calling $results->links()
.
Method A: Pass view name directly to the ->links()
method.
<div>
@foreach ($posts as $post)
...
@endforeach
{{ $posts->links('custom-pagination-links-view') }}
</div>
Method B: Override the paginationView()
method in your component.
class ShowPosts extends Component
{
use WithPagination;
...
public function paginationView()
{
return 'custom-pagination-links-view';
}
...
}
Unfortunately, Livewire will overwrite a custom view you have defined inside a service provider using:
Paginator::defaultView()
.
← Previous Topic
Authorization
Next Topic →