Skip to content
Advertisement

Laravel Livewire Pagination

Laravel – 8.6.0 (Jetstream), Livewire – 2.2.7

Among other stuff, I have two simple tables with search input rendered with Livewire. Both Livewire components have almost the same logic, the main difference is DB query format. Everything is working perfectly in one table (search, pagination), but in the second table, there’s a problem with pagination.

What’s wrong? When I click on the pagination link (eg. from first-page go to page(2)), the request is sent, the URL in the browser changes to a proper query string, a response is received, but there’s no HTML part in the response, there’s no change in the DOM and pagination shows the first page is active. If I manually refresh the page, the table shows data for page 2, and after that, I can even get new data for the next or previous page, but only once after the initial page refresh. If I click on pagination links again, no HTML data in response, no DOM change …

What have I tried?

  1. I’ve gone through the Livewire documentation, tried all suggested solutions from the “troubleshooting” section.
  2. I’ve checked my code multiple times, compared both Livewire components and Blade views, haven’t been able to find some obvious mistake.
  3. For the past 10 days, I’ve been searching for the answer on SO, Livewire forum, Laracast forum, … found some good suggestions, tried most of them, but none of them solves my issue.
  4. Eliminated the possibility that logic for DB query causes some issues, problem exits even if I don’t use complex record filtering (eg. if I just paginate all records – Model::all()->paginate(10)).
  5. Problematic table has some additional record filtering via radio buttons, I’ve temporarily removed that, but the problem remains.

Code for the working Livewire component and Blade view:

JavaScript
JavaScript
JavaScript

Code for the NON-working Livewire component and Blade view:

JavaScript
JavaScript
JavaScript

While I’m waiting for any suggestion, I’ll try one more clean Laravel/Jetstream/Livewire install. Thanks in advance, any help is much appreciated!

Advertisement

Answer

By looking at my code one more time, I’ve noticed some “leftovers” in model classes. In model class there shouldn’t be “use WithPagination;” Livewire trait! When I removed “use” statemnets and cleared view cache, everything is working OK!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement