The issue with your query is that the when method is not properly scoped. The when method applies the given callback only if the first argument is true. In your case, the first argument is $this->searchQuery != '', which means the callback will be executed regardless of the club_id.
To fix this issue, you need to include the club_id condition inside the callback function of the when method. Here's the updated query:
$members = Member::when($this->searchQuery != '', function ($query) {
$query->where('club_id', $this->club_id)
->where(function ($query) {
$query->where('first_name', 'like', '%' . $this->searchQuery . '%')
->orWhere('last_name', 'like', '%' . $this->searchQuery . '%')
->orWhere('number', $this->searchQuery);
});
})
->orderBy('active', 'desc')
->paginate(25);
By moving the where('club_id', $this->club_id) inside the callback function, the search conditions will only be applied if the search query is not empty. This ensures that the search is limited to members belonging to the currently viewed club.
Let me know if you have any further questions!