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

Manouj's avatar
Level 13

Livewire search not rendering properly first query

Hi, I am creating a basic search for products in the database using Livewire, with a custom page in FilamentPHP, which is essentially a Livewire page component.

The problem: When I enter the first query, it displays the results, but it doesn’t hide the previous ones. Also, when I hover over each previous search result component, it disappears. Note: This only happens for the first query. When I enter a new query, everything works fine. Could you help me fix this?

here is the code the component class :

public $query = '';

 protected function getViewData(): array
    {
        return [
            'products' => Product::where('product_name', 'ILIKE', '%' . $this->query . '%')
                ->get()
        ];
    }

and this is the code in the view

<x-filament::input.wrapper suffix-icon="heroicon-o-magnifying-glass" class="w-full">
                        <x-filament::input
                            class="py-3"
                            type="text"
                            wire:model.live.debounce.300ms="query"
                            id="search-input"
                            placeholder="Search for products"
                        />
</x-filament::input.wrapper>
<div id="products" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-2 xl:grid-cols-4 gap-5 w-full overflow-hidden">
                    @if ($products->count() > 0)
                        @foreach($products as $index => $product)
                            <livewire:product-item
                                :product="$product"
                                :key="'product-'.$product->id"
                            />
                        @endforeach
                    @else
                        <div wire:key="no-result" class="col-span-full text-center text-gray-500">
                            <div class="flex flex-col gap-4">
                                No products found.
                                <x-filament::link class="text-primary-400" :href="route('filament.mendlo.resources.products.create')">Create Product
                                </x-filament::link>
                            </div>
                        </div>
                    @endif
                </div>
0 likes
3 replies
LaryAI's avatar
Level 58

The issue you're experiencing with the Livewire search not rendering properly on the first query might be related to how the Livewire component is handling the state and rendering updates. Here are a few steps and suggestions to help you resolve this issue:

  1. Ensure Proper State Management: Make sure that the $query property is properly initialized and updated. Since you're using wire:model.live.debounce.300ms, Livewire should be updating the $query property correctly, but it's good to double-check.

  2. Check for JavaScript Errors: Sometimes, JavaScript errors can cause unexpected behavior in Livewire components. Open your browser's developer console and check for any errors that might be occurring when you perform the search.

  3. Use wire:key Properly: Ensure that each item in your loop has a unique wire:key. This helps Livewire track the components correctly. You seem to be doing this with :key="'product-'.$product->id", which is good.

  4. Debugging the Rendering: Add some debugging output to see if the products are being fetched correctly on the first query. You can do this by logging the products count or the query itself.

  5. Livewire Lifecycle Hooks: Consider using Livewire lifecycle hooks to debug or manipulate the component's behavior. For example, you can use updatedQuery to log when the query is updated.

  6. Check for CSS/JS Conflicts: Ensure that there are no CSS or JavaScript conflicts that might be causing the display issues. Sometimes, CSS transitions or JavaScript libraries can interfere with Livewire's DOM updates.

Here's a refined version of your component class and view with some debugging:

Component Class:

public $query = '';

protected function getViewData(): array
{
    $products = Product::where('product_name', 'ILIKE', '%' . $this->query . '%')->get();

    // Debugging: Log the number of products found
    logger()->info('Products found: ' . $products->count());

    return [
        'products' => $products
    ];
}

View:

<x-filament::input.wrapper suffix-icon="heroicon-o-magnifying-glass" class="w-full">
    <x-filament::input
        class="py-3"
        type="text"
        wire:model.live.debounce.300ms="query"
        id="search-input"
        placeholder="Search for products"
    />
</x-filament::input.wrapper>
<div id="products" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-2 xl:grid-cols-4 gap-5 w-full overflow-hidden">
    @if ($products->count() > 0)
        @foreach($products as $index => $product)
            <livewire:product-item
                :product="$product"
                :key="'product-'.$product->id"
            />
        @endforeach
    @else
        <div wire:key="no-result" class="col-span-full text-center text-gray-500">
            <div class="flex flex-col gap-4">
                No products found.
                <x-filament::link class="text-primary-400" :href="route('filament.mendlo.resources.products.create')">Create Product
                </x-filament::link>
            </div>
        </div>
    @endif
</div>

If the issue persists, consider creating a minimal reproducible example and testing it in isolation to rule out any external factors. This can help you identify if the problem is specific to your setup or a broader issue with Livewire or FilamentPHP.

Snapey's avatar

check the browser source and make sure your wire:key is being rendered uniquely for each item

Manouj's avatar
Level 13

@Snapey in the browser source there is a unique wire:id in the loop elements but there isn't wire:key at all in loop elements

I also tried this :

@foreach($products as $index => $product)
                                <livewire:product-item
                                    :product="$product"
                                    wire:key="product-{{$product->id}}"
                                />
@endforeach

same problem

Please or to participate in this conversation.