You should always group orWhere calls in order to avoid unexpected behavior when global scopes are applied.
Sep 7, 2022
5
Level 10
Eloquent query with filter and search
I want my search query to filter the results via dropdown and search input. Somehow the filter does't apply to my query. Maybe someone can help me out?
use WithPagination;
public $status;
public $search;
protected $listeners = ['refreshComponent' => '$refresh'];
protected $queryString = ['search', 'status' => ['except' => '']];
public function setStatus($status = "")
{
$this->status = $status;
$this->updatingSearch();
}
public function updatingSearch()
{
$this->resetPage();
}
public function render()
{
$jobs = Job::where('user_id', auth()->user()->id)->with('user')->where(function ($query) {
//drop down job status
if($this->status == 'active') {
$query->where('time_of_use', '>', Carbon::now());
} else if ($this->status == 'inactive') {
$query->where('time_of_use', '<', Carbon::now());
}
$query->where('name', 'like', '%' . $this->search . '%')
->orWhere('email', 'like', '%' . $this->search . '%')
->orWhere('title', 'like', '%' . $this->search . '%');
})->orderBy('created_at', 'DESC')->paginate(20);
return view('livewire.job.jobs', ['jobs' => $jobs]);
}
What I am doing wrong here? Filter gets applied. Then it should be possible for example to only search the active results.
Level 75
1 like
Please or to participate in this conversation.