Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ryuk.toby@gmail.com's avatar

'Roles and Permissions' Code Problem

Hi, I'm looking at the 'Roles and Permissions' video and when I try to add a permission check in the 'boot' function in the 'AuthServiceProvider.php' it gives me the error 'Undefined variable: permission'. I'm using Laravel 5.2

This is the permission check thingy:

foreach ($this->getPermissions() as $permission) {
            $gate->define($permission->name, function ($user) {
                $user->hasRole($permission->roles);
            });
        }       

And here's the getPermissions

protected function getPermissions()
    {
        return Permission::with('roles')->get();
    }
0 likes
3 replies
d3xt3r's avatar
d3xt3r
Best Answer
Level 29

Watch the complete video :)

foreach ($this->getPermissions() as $permission) {
            $gate->define($permission->name, function ($user) use($permission) {
                 return $user->hasRole($permission->roles);
            });
        }   
ryuk.toby@gmail.com's avatar

Oh shit, I feel so stupid now.... Of fucking course it's right after where I paused.....

Please or to participate in this conversation.