I create a role-based project with Laravel 5 and Entrust, everything works fine until I try to update a user's role list, the bloody "Integrity constraint violation" reminds me that when calling
$user->attachRoles($roles);
It does not delete the user's roles first, so I check the interfaces of usertrait,then come up with
$user->detachRoles($user->roles);
$user->attachRoles($roles);
The code looks pretty ugly, and I think there is three SQL been executed
I was really hoping it would be like
$user->updateRoles($roles);
which can compare existing roles, and do some optimazation.
Am I missing something?
You can use function $user->roles()->sync($roleKeys)
. It takes array of role keys and after executing all previous roles are deleted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With