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

Fzoltan87's avatar

Livewire Search

Hi everyone,

I have a search form that displays the search results when I click the search button. However, I have an issue: when I add a city, the search is triggered. I can add multiple cities to the search at the same time. If I remove the added city, the search is also triggered. How can I prevent this from happening?

Here is the source code:

app\Livewire\PropertyList.php

resources\views\livewire\property-list.blade.php

0 likes
4 replies
Snapey's avatar

render is called after any change if your fields are live. You need a way to indicate that the search is ready to be performed, maybe with a search button. If you know search has been pressed then you can do the search so that the render method can show the results, and not query on every render.

Fzoltan87's avatar

@Snapey

Hi, Are you suggesting that I should introduce a status with a default value of "false," and it would be set to "true" when the search button is clicked? Perhaps an example code would help clarify things better in case we're not thinking of the same solution.

bvfi-dev's avatar
bvfi-dev
Best Answer
Level 3

@Fzoltan87 I think this should work, because this would trigger a query only when needed:

public $shouldSearch = false;
public $searchResults; // Query properties

public function applyFilters()
{

    $this->shouldSearch = true; // User presses search button
    $this->resetPage(); //Not sure if this is the correct order for this to execute here, do tests if something isn't refreshing right
    $this->searchResults = $this->filteredPropertyQuery()->paginate(12); // Load query results here
}

public function render()
{
    
    return view('livewire.property-list', [
        'properties' => $this->shouldSearch // Only run the query if $shouldSearch is true
            ? $this->searchResults
            : collect(), // or empty collection
        'property_types' => PropertyType::visible()->get(['id', 'name']),
        'property_categories' => PropertyCategory::visible()->get(['id', 'name']),
        'property_location_cities_names' => $this->getSelectedCityNames(),
    ]);
}

Then to display them

@if ($properties->count())
    @foreach($properties as $property)
        {{--  --}}
    @endforeach
@else
    <p>No properties</p>
@endif
1 like

Please or to participate in this conversation.