I'm trying to loop a query like the one in the title in laravel. Every cycle is a new "AND (x OR y OR z)" condition.
Here is what I have:
$offers = Offer::query();
foreach($values as $val){
$offers->where('title', 'LIKE', '%'.$val.'%')
->orWhere('company', 'LIKE', '%'.$val.'%')
->orWhere('location', 'LIKE', '%'.$val.'%')
->orWhere('country', 'LIKE', '%'.$val.'%')
->orWhere('country_code', 'LIKE', '%'.$val.'%');
}
$offers->get();
However this doesn't do what I want.
I also tried this:
$offers = Offer::query();
foreach($values as $val)
{
$offers->where(function($query, $val) {
$query->where('title', 'LIKE', '%'.$val.'%')
->orWhere('company', 'LIKE', '%'.$val.'%')
->orWhere('location', 'LIKE', '%'.$val.'%')
->orWhere('country', 'LIKE', '%'.$val.'%')
->orWhere('country_code', 'LIKE', '%'.$val.'%');
});
}
$offers = $offers->distinct()->get();
But this gives me an error at the $offers->where(function($query, $val)
line: Missing argument 2 for App\Http\Controllers\OffersController::App\Http\Controllers\{closure}()
I could easily write the raw SQL query in the loop, but I really would like to use Laravel's ORM here.
Any idea how to achieve that? Thanks!