In PHP, By default, a function has no access to the variables declared outside of the function.
This variable can not be accessed from that closure
public function hasRole($role = null)
{
$hasRole = false;
$hasRole = !$this->roles->filter(function($item) {
return $item->role == $role;
})->isEmpty(); ^^^^^
return $hasRole;
}
You can use the use
keyword, which passes one variable to a function:
public function hasRole($role = null)
{
$hasRole = false;
$hasRole = !$this->roles->filter(function($item) use($role) {
return $item->role == $role;
})->isEmpty();
return $hasRole;
}
They is more info about this particular problem on this blog post