I want to write an eloquent in Laravel, but I don't know how.
I have an array which contains some words:
$words = ["word1", "word2", "word3", ...]
And I have a table like this:
+--+--------+--------+----------+
|id| name1 |name2 |is_active |
+--+--------+--------+----------+
| 1| smthng1| anthng1| 1 |
| 2| smthng2| anthng2| 1 |
| 3| smthng3| anthng3| 0 |
| 4| smthng4| anthng4| 1 |
I want to write an eloquent which is equal to this query:
select * from `books`
where `is_active` = 1
and (`name1` like "%word1%" or `name2` like "%word1%")
and (`name1` like "%word2%" or `name2` like "%word2%")
and (`name1` like "%word3%" or `name2` like "%word3%")
I have such an eloquent if the $words is just a single string:
$books = Books::where('is_active','1')->where(
function($query) use ($searchWords){
$query->where('name1', 'like', "%$searchWords%")
->orWhere('name2', 'like', "%$searchWords%");
}
)
->get();
But I don's know what to do if the $words is an array of strings.
Thanks in advance