forumium icon indicating copy to clipboard operation
forumium copied to clipboard

cant delete user

Open svstoya-cyber opened this issue 6 months ago • 1 comments

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))

svstoya-cyber avatar Aug 19 '25 11:08 svstoya-cyber

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();
        });
    }

svstoya-cyber avatar Aug 19 '25 11:08 svstoya-cyber