It works with the same User model and UsersTest unit test when I place the following code into app/filters.php:
Event::listen('eloquent.deleting: User', function($user) {
// sdv_dbg('eloquent.deleting: User');
// sdv_dbg('User',$user);
$user->userRoles()->delete();
});
Why does not the next one works? Is it a framework bug or I do not understand something?
public static function boot() {
parent::boot();
static::creating( function($user) {
if ( !$user->isValid()) {
return false;
}
});
static::deleting( function($user) {
$user->userRoles()->delete();
});
}
Do you have any foreign keys on that table or from that table?
It could be the DB is enforcing a foreign key and won't let you delete a parent without destroying the children first.
No, migrations do not define DB foreign keys. There are only Eloquent "virtual" foreign keys in ->hasMany() and ->belongsTo(). I came from MediaWiki background, where foreign keys are not used. Unit tests related to adding / deleting relations pass completely. Only ::deleting() inside ::boot() does not work. I am using 4.2.8.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community