How can I prevent detaching of roles on user soft delete?
$user->hasRole('subscriber')
=> true
$user->delete()
$user->hasRole('subscriber')
=> false
$user->restore()
$user->hasRole('subscriber')
=> false
Asked
Active
Viewed 538 times
-1

fiter
- 743
- 1
- 12
- 25
1 Answers
0
Look at EntrustUserTrait rows 69-80.
/**
* Boot the user model
* Attach event listener to remove the many-to-many records when trying to delete
* Will NOT delete any records if the user model uses soft deletes.
*
* @return void|bool
*/
public static function boot()
{
parent::boot();
static::deleting(function($user) {
if (!method_exists(Config::get('auth.model'), 'bootSoftDeletes')) {
$user->roles()->sync([]);
}
return true;
});
}
I think you do not use Laravel's own SoftDeletes trait if you have not bootSoftDeletes.
class User extends Authenticatable
{
use SoftDeletes;
use EntrustUserTrait;
...

taju
- 1
- 2