Mounir Elfassi Mounir Elfassi - 5 months ago 23x
SQL Question

"IN" query in Laravel 5.2

i'm trying to get this query :

$my_query = "select * from tableuser where `userid`<> '$userid' and userid in
(select friend from userrelations where blocked='no' and user_id='$userid')";

to work in
Laravel 5.2

what i tried so far :

$query_first = DB::table('userrelations')
->where('blocked' , '=', 'no')
->where('user_id' , '=', $userid)

$my_query = DB::table('tableuser')
->where('userid' , '<>', $userid)
->where('userid' , 'in', $query_first)

by doing this i get empty result, which is not correct.

any help please?


For first query you should replace




With pluck() you'll get an array of ids instead of array of objects.

Then, incorrect part in second query:

->where('userid' , 'in', $query_first)

There's whereIn():

$users = DB::table('tableuser')->whereIn('userid', $query_first)->get();