How to search clients of the authenticated user?

Solution 1:

You're mixing your and/or clauses. You want to make sure that user_id is always respected, but the rest are maybes. To fix that, pass the or clauses into a closure:

return view('livewire.dashboard.user-management', [
    'clients' => Client::where('user_id', '=', $user)
        ->where(function($query) {
            $query->where('name', 'LIKE', '%'.$this->searchTerm.'%')
                ->orWhere('surname', 'LIKE', '%'.$this->searchTerm.'%')
                ->orWhere('email', 'LIKE', '%'.$this->searchTerm.'%');
        })
        ->orderBy($this->sortField, $this->sortAsc)->paginate(8)
        
]);