I'm trying to execute IN statement in Eloquent query. I tried this:
$id = urldecode($id);
$news = News::whereHas('newsCategories', function($q) use($id)
{
$q->where('category_id', 'IN', '('.$id.')')->orderBy('created_at', 'desc');
})->get();
But I get empty result.
When I switch IN
for =
and pass a number instead of array I get results. Did I do something wrong?
So this works:
$id = 2;
$news = News::whereHas('newsCategories', function($q) use($id)
{
$q->where('category_id', '=', $id)->orderBy('created_at', 'desc');
})->get();
You want
whereIn
with an array of your ids to be checked...Just make sure that you're passing in something that is truly an array :)