Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.