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

MahmoudAdelAli's avatar

Search with Live-Wire

Hi , there's a junior at my team asking me for code-review for his Article - Search so i decided to share the code with you to see if there's any comment we can share it with him .

Another side to fire the event

class BlogSearch extends Component
{
    public $query;

    public function updatedQuery()
    {
        $this->dispatch('search:updated', $this->query);
    }
    public function render()
    {
        return view('livewire.blog-search');
    }

}

the blade is simple

#Blogs.blade 
## SearchComponent
#ForElse direct at Blogs.blade

So what you think ?

0 likes
1 reply
LaryAI's avatar
Level 58

The code provided for the Livewire search functionality is generally well-structured, but there are a few improvements and clarifications that can be made to enhance its readability and functionality. Here's a review and some suggestions:

  1. Event Dispatching: The use of dispatch in the BlogSearch component to trigger the search:updated event is a good approach. However, ensure that the event name is consistent and correctly handled in the Blogs component.

  2. Event Listener: The #[On('search:updated')] attribute in the Blogs component is not a standard Livewire feature. Instead, you should use Livewire's built-in event listening capabilities. You can listen for events using the protected $listeners property.

  3. Code Consistency: Ensure that the arrow functions (->) are correctly formatted. It seems like there might be some encoding issues in the code snippet provided (e.g., -> should be ->).

  4. Blade Template: Ensure that the Blade templates are correctly set up to handle the search input and display the results. The Blogs.blade.php should include a search input field that binds to the query property in the BlogSearch component.

Here's a revised version of the code with these suggestions:

// BlogSearch.php
use Livewire\Component;

class BlogSearch extends Component
{
    public $query;

    public function updatedQuery()
    {
        $this->emit('searchUpdated', $this->query);
    }

    public function render()
    {
        return view('livewire.blog-search');
    }
}
<!-- blog-search.blade.php -->
<div>
    <input type="text" wire:model="query" placeholder="Search blogs...">
</div>
<!-- blogs.blade.php -->
<div>
    @livewire('blog-search')

    <div>
        @forelse($blogs as $blog)
            <div>{{ $blog->name }}</div>
        @empty
            <div>No blogs found.</div>
        @endforelse

        {{ $blogs->links() }}
    </div>
</div>

Key Points:

  • Event Handling: Use $listeners to handle events in Livewire components.
  • Blade Components: Ensure the search input is correctly set up to bind to the query property.
  • Pagination: Ensure pagination links are displayed using {{ $blogs->links() }}.

These changes should help improve the functionality and maintainability of the Livewire search feature.

Please or to participate in this conversation.