How to start a query in Laravel with Eloquent?
Asked Answered
E

1

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!

Eventide answered 26/10, 2016 at 21:0 Comment(5)
Show all code from modelDisregard
$query = Model::query();Admonish
Thanks user2094178! That works! <3Eventide
@Admonish You may want to use your comment as an answerHeadspring
Thanks for the info :DColeencolella
E
11

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

$query = App\User::query();
Emaciation answered 3/6, 2018 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.