When I use whereIn method with DB::raw, result is always empty.
Here is my example code;
print_r(
DB::table('posts')
->whereIn('id', array( DB::raw('SELECT post_id FROM related_posts') ))
->get()
);
Query and bindings var_dump results;
string 'select * from `news` where `id` in (SELECT news_id FROM related_news)' (length=69)
array (size=1)
0 =>
object(Illuminate\Database\Query\Expression)[128]
protected 'value' => string 'SELECT news_id FROM related_news' (length=32)
When I try this SQL with MySQL client, I get results.
Maybe I'm using wrong method for where in with sql query. Is this bug? Or how can I do this query truly.
Hi, have you tried using the whereRaw function?
->whereRaw('id IN (SELECT post_id FROM related_posts)')
Thanks, that's what i'm looking for. But this still be a bug I guess.
Most helpful comment
Hi, have you tried using the whereRaw function?