I have a parent child relation in users.
I want to delete a child. But before that, I need to remove all references from the parents to the child.
I was expecting this to work - but is not:
$child->superiorUsers()->detach($child->id);
$child->delete();
The superiorUsers() looks like this:
public function superiorUsers() { return $this->belongsToMany( 'App\Models\User', 'user_user', 'user_id', 'superior_id' ); }
Any idea what I am doing wrong please?
Edit: I am writing unitTests to delete the user and I am getting the error that the relation is still there.
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (
user_user
, CONSTRAINTuser_user_user_id_foreign
FOREIGN KEY (user_id
) REFERENCESusers
(id
)) (SQL: delete fromusers
whereid
= xxx)
onDelete('cascade')
oronDelete('set null')
to the constraint or use the eloquentdeleting
event. – IGP