Reputation: 157
I have an array of keywords:
arr = ["puppy", "kitty", "baby"];
I want to have Laravel Eloquent query where id < 20 and that 'subject' has one or more of the array values.
Not sure how to write this. I tried foreach but didn't work:
foreach($keywords as $keyword) {
$firstPageTopics = Article::where('id', '<', 20)->where('type', 'LIKE', '%'.$keyword.'%')->get();
}
Upvotes: 2
Views: 1280
Reputation: 50787
You can do the loop inside the closure
of a where
and use the orWhere
method to chain several conditional where's together.
Article::where('id', '<', 20)
->where(function ($q) use ($keywords) {
collect($keywords)->each(function ($keyword) use ($q) {
$q->orWhere('type', 'like', '%'. $keyword .'%');
});
})->get();
Upvotes: 4