Zaw Myo Htet
Zaw Myo Htet

Reputation: 540

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.

Upvotes: 2

Views: 2125

Answers (1)

Maerlyn
Maerlyn

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

Related Questions