Support the ongoing development of Laravel.io →
Scout Eloquent Testing
Last updated 10 months ago.
1

Hi Lucas.

You should be able to use the conditional clause on the builder to achieve this.

Your use-case would be something like this (untested):

$users = User::when($this->search, function ($query) {
    $query->search($this->search);
})
    ->orderBy($this->orderBy, $this->orderAsc ? 'ASC' : 'DESC')
    ->paginate($this->perPage);
2

Sign in to participate in this thread!

LaraJobs

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 Laravel.io - All rights reserved.