I've tried and followed the steps in installing this package.
One thing I've noticed is that, when adding a direct permission to a user, the changes don't reflect immediately.
I did this in laravel tinker.
$user->getAllPermissions()delete articles is nowhere to be found.But when I restarted laravel tinker and fetched all permissions from the same user, it appeared.
Is there something I'm doing wrong or is there already a workaround for this?
Thank you!
I think you're just running into basic Eloquent model "state".
You've created a $user object.
Then you used this package to add permissions to "the user", but you didn't update the $user object itself.
When this package adds permissions to the user, it doesn't update that object in memory. You need to call fresh() on the $user object to get the updated associated properties.
$user->fresh()->getAllPermissions() will show the updated permissions.
If you want to actually update the $user object, use $user = $user->fresh();
Ok, it worked. But why can't I use the can function with the direct permission I just gave to a user? Noting that I've already done $user = $user->fresh();.
I can see the direct permission when calling getAllPermissions() on the user, but when I use $user->can('direct permission'), it always return false.
Hi @wreighsantos
This is because can uses Laravel's gate. We only register all permission to the gate once during app boot (in PermissionRegistrar@registerPermissions).
If you really need the can method to work right after you assigned a permission to a user you could use app(PermissionRegistrar::class)->registerPermissions() to re-register all permissions on Laravel's gate immediately.
This will be fixed in v3 or when #505 gets merged.
Try
app(PermissionRegistrar::class)->forgetCachedPermissions()
work perfectly for me
Most helpful comment
I think you're just running into basic Eloquent model "state".
You've created a
$userobject.Then you used this package to add permissions to "the user", but you didn't update the
$userobjectitself.When this package adds permissions to the user, it doesn't update that object in memory. You need to call
fresh()on the$userobject to get the updated associated properties.$user->fresh()->getAllPermissions()will show the updated permissions.If you want to actually update the
$userobject, use$user = $user->fresh();