Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined variable inside laravel left join

in my case, i want to display users and orders completed by each user, orders have status i get $status from input form here is my code

$orders = DB::table('user_profiles')
        ->leftJoin('orders', function($join){
            $join->on('user_profiles.id','=','orders.id_user')
                ->where('orders.status','=',$status);
        })
        ->selectRaw('user_profiles.*, count(orders.id_user) as order_try_count')
        ->groupBy('user_profiles.id')
        ->orderBy('order_try_count',$order)
        ->paginate(15);

but i get undefined variable status, what should i do to solve this problem ?, thank you

like image 509
Zezen Avatar asked Dec 15 '22 06:12

Zezen


1 Answers

Yoo need to pass variables into closure using use construction like so:

->leftJoin('orders', function($join) use ($status) {

instead of

->leftJoin('orders', function($join) {

Reference: anonymous functions

like image 94
Marcin Nabiałek Avatar answered Dec 25 '22 11:12

Marcin Nabiałek