Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

unlikenesses's avatar

Listen to relationship events

Is there a way to listen to when the user attaches/detaches a model in Nova?

0 likes
7 replies
unlikenesses's avatar

Thanks, I hadn't heard of that package. Unfortunately I've tried it and it doesn't work with Nova. I know the package is working by using tinker, but the events aren't triggered by Nova.

D9705996's avatar

@unlikenesses - I dont think this package has anything to do with tinker. It simply adds additional model events for relationships to the core events.

Did this work for you in a non-nova environment?

Say you have a User and Comment with a hasMany relationship (one user has many comments) and belongsTo inverse (A comment belongs to a User) you should be able to add this to your User model

protected static function boot()
    {
        parent::boot();

        static::belongsToAssociated(function ($relation, $related, $parent) {
            Log::info("User has been associated with comment {$parent->id}.");
        });
    }

When you add a new Comment you should see an entry in your logs. If this works without Nova then I would try seeing if when you add a comment in Nova admin panel you see another log message

unlikenesses's avatar

I meant that I used tinker to check that the events were firing. And they were. So yes it did work for me in a non-nova environment.

I added logging to the listeners, and they are triggered in a non-nova environment (tinker), but they are not triggered in Nova.

unlikenesses's avatar

Thanks for your persistence. You don't need to use Model Observers.

The problem therefore remains. I appreciate your efforts anyway, thanks again @D9705996!

1 like

Please or to participate in this conversation.