简体   繁体   中英

Using variable in laravel 4.1 Query Builder

How can use variable in laravel query builder. Here is my code.

$role = 1;
$user = DB::table('users')
                    ->join('assigned_roles', function($join)
                    {
                        $join->on('users.id', '=', 'assigned_roles.user_id')
                             ->where('assigned_roles.role_id', '=', $role );
                    })
                    ->get();

But it return Undefined variable: role. How can I solve this problem. Help me plz.

You need to import variables from the local scope to the anonymous function's scope:

function ($join) use ($role) {}

See the example in the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM