forumium
forumium copied to clipboard
cant delete user
I am logged in as "Administrator". I went to the "Users" page (there was no search on this page), clicked "Edit" on a user, and then the delete confirmation modal popped up. I clicked "Confirm", and got the following error:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (forum.user_roles, CONSTRAINT user_roles_user_id_foreign FOREIGN KEY (user_id) REFERENCES users (id))
I tried to add a deleting event in the User model’s boot method, but it doesn’t work. What should I do?
public static function boot()
{
parent::boot();
static::created(function (User $item) {
UserRole::create([
'role_id' => Role::where('name', RoleConstants::MEMBER->value)->first()->id,
'user_id' => $item->id
]);
});
static::deleting(function ($user) {
$user->roles()->delete();
});
}