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

6ber6ou's avatar

ACL Role Permission and "Undefined variable: permission"

Hi all.

I made the Jeffray's tutorial "ACL roles and permissions" but I have this error message :

ErrorException in AuthServiceProvider.php line 42:
Undefined variable: permission (View: /home/vagrant/WebSites/cmne-  viewer/resources/views/home.blade.php)

That's happend when I use @can( 'create_post' ) in the view

0 likes
3 replies
coder's avatar

post some more code specially the AuthServiceProvider and all.

bombrill's avatar

I have the same problem :

AuthServiceProvider :

public function boot(GateContract $gate)
    {
        $this->registerPolicies($gate);
        
        
        foreach($this->getPermissions() as $permission) {
            $gate->define($permission->name, function($user) {
                $user->hasRole($permission->roles);
            });
        }
    }
    
    protected function getPermissions() {
        return Permission::with('roles')->get();
    }
ShaneDRosenthal's avatar

Change the function to use ($permission) like this:

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

Please or to participate in this conversation.