Ad

Laravel Query Builder WHERE NOT IN With Multiple WHERE Clause

- 1 answer

I have following query,

SELECT * FROM `users` WHERE approved='1' 
and users.id NOT IN (SELECT donations.user_id FROM donations where month='1' 
and year='2016');

I have followed Laravel Query Builder WHERE NOT IN and converted my query like this,

$users = DB::table('users')->where('approved','=',1)->whereNotIn('id', function($q){
        $q->select('user_id')->from('donations')->where('month','=','$month')->where('year','=','$year');
        })->get();

But its not working, can anybody help me out where i am making mistake. how can I convert it into Laravel query builder format? Thanks.

Ad

Answer

You shouldn't put your $month and $year variables in quotes.

Replace

->where('month','=','$month')->where('year','=','$year')

with

->where('month','=',$month)->where('year','=',$year)
Ad
source: stackoverflow.com
Ad