Framework: whereIn and DB::raw problem

Created on 26 Apr 2013  路  2Comments  路  Source: laravel/framework

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.

Most helpful comment

Hi, have you tried using the whereRaw function?

->whereRaw('id IN (SELECT post_id FROM related_posts)')

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digirew picture digirew  路  3Comments

felixsanz picture felixsanz  路  3Comments

kerbylav picture kerbylav  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments