Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rdg's avatar
Level 1

Help with filters and dynamic sorting in Livewire/Laravel

I am using Livewire, Laravel, and Alpine.js/Choices.js for some UI components.

There is a table that displays vehicle records. I seeded the local database with 3,000 vehicles, but the page freezes when I try to apply a filter. I’m only showing parts of the code here. The filters are dependent, meaning when the user selects an option in one filter, all other filters must update to show only options associated with vehicles that match the selected criteria.

There are 14 filter options in total. There are 8 selects options with model like fuel, origin. There are 4 input that the user can write prices, kms or registration date. And a search bar. getFuelOptions is a exemple, there is a function like this for every select.

Here is the code for my Livewire component:

This is the back end logic on the component. For the blade.php file, i used choices to show the filters in selects

How can I optimize this process to avoid freezing and improve performance?

0 likes
3 replies
kleninmaxim's avatar

It seems that your logic is difficult. I suggest to use filtering may be not on sql but on php and add caching that logic

May be also do refactoring code to simplify code, because now it hard to read and find the problems

Sinnbeck's avatar

If the page freezes and you are doing the request using ajax, I dont see any reason it would be php that is the problem (unless you make an ajax request and then try refreshing the page, and are using php artisan serve as it is single threaded)

Unsociable's avatar

Looking at some of your queries, they could do with some improvement, you can expand where functions maybe with some of your foreach loops? You could start by commenting out some queries which you think can cause the issues to pinpoint it. Also include try catch's in places and if you moved your code out to a $query at the start, you can add debug line by line to see where the code ran too.

$query->where(function() {
} );

Please or to participate in this conversation.