9

I'm trying to write the following code:

foreach($relations as $relation){
    $query->orWhereHas($relation, function ($query) use ($filter) {
        $query->where('name', 'like', '%'.$filter.'%');
    });
}

but I don't know how to initialize the $query variable, I'd prefer not to write the first orWhereHas before the foreach starts. Anyone knows how to achieve this? Thanks!

5
  • Show all code from model Commented Oct 26, 2016 at 21:14
  • 13
    $query = Model::query(); Commented Oct 26, 2016 at 21:17
  • Thanks user2094178! That works! <3 Commented Oct 26, 2016 at 21:23
  • @user2094178 You may want to use your comment as an answer Commented Apr 5, 2018 at 20:02
  • Thanks for the info :D Commented May 18, 2018 at 10:39

1 Answer 1

12

Eloquent models has query() method. Then, you can do this

$query = App\User::query();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.