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

lara28580's avatar

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.

0 likes
5 replies
lara28580's avatar

@MichalOravec Thanks for the reply!

You mean it should look like this, right?

$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(function ($query) {
                $query->where('name', 'like', '%' . $this->search . '%')
                ->orWhere('email', 'like', '%' . $this->search . '%')
                ->orWhere('title', 'like', '%' . $this->search . '%');
            });
            
        })->orderBy('created_at', 'DESC')->paginate(20);
tykus's avatar

@SmokeTM the outer grouping is unnecessary, and some tidy-up:

$jobs = Job::with('user')
    ->where('user_id', auth()->id())
    ->when($this->status == 'active', fn ($query) => $query->where('time_of_use', '>', now()))
    ->when($this->status == 'inactive', fn ($query) => $query->where('time_of_use', '<', now()))
    ->where(function ($query) {
        $query->where('name', 'like', '%' . $this->search . '%')
            ->orWhere('email', 'like', '%' . $this->search . '%')
            ->orWhere('title', 'like', '%' . $this->search . '%');
    })
    ->latest()
    ->paginate(20);
1 like

Please or to participate in this conversation.