Skip to content

Observer does not listen to "trashed" event from Model #54980

@duemti

Description

@duemti

Laravel Version

12

PHP Version

8.3.6

Database Driver & Version

No response

Description

From my double checking the source code, the soft deletion of a model triggers "deleted" and "trashed" events.
But in my Observer for the Model i only catch the "deleted" event.
I tested with a direct Listener in a Providers boot method and it did catch the "trashed" event.

Why the Observer is not catching the "trashed" event?

Steps To Reproduce

Create a Model with Soft Delete Trait.

Create a Observer for the Model.

Register the Observer using ObservedBy attribute in Model class.

Add "public function trashed(Model $object): void" method to Observer class.

Soft delete a record from DB using the Model with delete()

The trashed() method from Observer is not triggered by Model "trashed" event and is not executed.

EDIT:
For anyone interested, i solved this by adding $observables to the model class that has SoftDeletes Trait:

protected $observables = [
    'trashed',
];

Also a pull request #54987

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions