Reputation: 540
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.
Upvotes: 2
Views: 2125
Reputation: 34107
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.
Upvotes: 12