Just pass the $permissions
variable by reference:
$user->roles->each(function($role) use(&$permissions) { //<-- passed by reference
$permissions = array_merge($permissions, $role->perms->toArray());
});
Notice the &
, now you have the same variable declared outside the function and it's in your current scope and modified inside the closure. SO, you can use the $permissions
here, for example:
dd($permissions); // dump and die