How to build Pagination with Laravel Livewire

Laravel
July 11, 20201 minuteuserVishal Ribdiya
How to build Pagination with Laravel Livewire

Livewire is a very awesome thing that I have ever seen, the old school developers are still using the jquery and ajax concept to not refresh the page. But forget the jquery and ajax stuff. If you are good at PHP then you can do the same with Laravel Livewire.

Wait what?

Load dynamic data on the page without using ajax? Yes, it is possible with Laravel Livewire. So that is all about laravel livewire, and in this tutorial, we will see how to build laravel pagination with laravel livewire.

Let's start and I hope you have already set up the livewire. Let's say you already have created a component named UsersListing Now in the users listing, we want to paginate all users and we will list 10 records per page.

How to use pagination with Laravel Livewire

Livewire provides a trait called WithPagination and you have to add it into your component UsersListing. Check out the following code:

use Livewire\WithPagination; 
use Livewire\Component; 
class UsersListing extends Component 
{ 
   use WithPagination;

   public function render() 
     { 
       return view('livewire.users.index', [ 'users' => User::paginate(10), ]); 
     } 
} 

And to load pagination you have to add the following code:

@foreach ($users as $user) ... 

@endforeach {{ $users->links() }} 

That's it, and your laravel pagination now works like charm without page refresh. There is much more about pagination like how to use it with a custom view, how to use it with a custom theme. We will see it in our next tutorial, until then enjoy the code