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

armen_98's avatar

Question about ACL in Laravel: Roles and Permissions

Hey all! I get this error: Undefined variable: permission when I am using variable $permission inside a function like this

$gate->define($permission->name, function($user) {
       return $user->hasRole($permission->roles);
});

I've already read here the solution https://laracasts.com/discuss/channels/laravel/romanbicanroles-package-and-exception , but i want to know why I cant access permission variable without saying to function to use it? Thanks!

0 likes
2 replies
ABDELRHMAN's avatar
Level 5

from php docs

Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared

armen_98's avatar

Thanks a lot, that actually helped me

1 like

Please or to participate in this conversation.