Skip to content

cant delete user #23

Open
Open

Description

@svstoya-cyber

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

Activity

  1. added
    bug
    on Aug 19, 2025
  2. svstoya-cyber commented on Aug 19, 2025

    @svstoya-cyber
    Author

    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();
            });
        }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions