Laravel

Livewire Autocomplete Search

January 16, 2026 1 min read 12 views

Autocomplete search:

Component

public string $search = '';
public ?User $selected = null;

#[Computed]
public function results()
{
    if (strlen($this->search) < 2) return [];
    return User::where('name', 'like', "%{$this->search}%")
        ->limit(5)->get();
}

public function select(User $user)
{
    $this->selected = $user;
    $this->search = $user->name;
}

View

    @foreach($this->results as $user)
  • {{ $user->name }}
  • @endforeach
Share this post:

Related Posts

Comments (0)

Please log in to leave a comment. Log in

No comments yet. Be the first to comment!