Advanced nested AND clause in Laravel 5 Eloquent -
i having hard time converting query laravel eloquent , appreciated.
the query need run
select * `articles` `articles`.`deleted_at` null , `id` <> 2 , (`tags` '%tag1%' or `tags` '%tag2%') this have now
$relatedarticles = article::where('id', '<>', $article->id); if (!is_null($article->tags)) { foreach (explode(',', $article->tags) $tag) { $relatedarticles = $relatedarticles->orwhere('tags', 'like', '%' . $tag . '%'); } } $relatedarticles = $relatedarticles->get(); but above code yields me
select * `articles` `articles`.`deleted_at` null , `id` <> 2 or `tags` '%tag1%' or `tags` '%tag2%' which not looking for.
thanks
with regards gagan
you should use "advanced where". instead of column name feed where function. more clarity here http://laravel.com/docs/5.0/queries#advanced-wheres. code should 1 below:
$relatedarticles = article::where('id', '<>', $article->id); if (!is_null($article->tags)) { $relatedarticles->where(function($query) use ($article) { foreach (explode(',', $article->tags) $tag) { $query->orwhere('tags', 'like', '%' . $tag . '%'); } }); } $relatedarticles = $relatedarticles->get(); p.s. if you're wondering use is, anonymous functions on php not on javascript, need specify variables visible on function.
Comments
Post a Comment