P
P
photosho2017-03-03 09:28:33
Laravel
photosho, 2017-03-03 09:28:33

Is there an analogue of "where" with multiple fields?

Hello. The question is the following. There is a table with users, by which I am looking for a match of the "Last Name" column with a part of the substring:
User::where("surname", "like", "%str%")...
Now there is a need to look for a match of the same substring with several fields, for example, with the first name:

User::where("surname", "like", "%str%")->orWhere("name", "like", "%str%")...

So, is it possible to shorten the query, instead of using "orWhere" somehow passing multiple field values ​​for "where" at the same time? Tried it and didn't work:
User::where(["surname", "name"], "like", "%str%")...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel, 2017-03-03
@photosho

$query= User::query();
foreach (['surname', 'name'] as $item) {
    $query->orWhere($item, "like", '%str%');
}
$result = $res->get();

A
Andrew, 2017-03-03
@AndrewHaze

SQL IN Operator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question