troelskn / laravel-fillable-relations

Provides HasFillableRelations trait to Eloquent models
63 stars 23 forks source link

Idea: Raise custom event after save method #14

Open eporsche opened 5 years ago

eporsche commented 5 years ago

Currently there is no way to register a listener after both the model and its fiillable relations have been saved. I would like to propose something like: $model->fireHasFillableRelationsEvent('created_with_relations');

after the save method in the HasFillableRelations trait.

protected function fireHasFillableRelationsEvent($event)
    {
        if (! isset($this->dispatchesEvents[$event])) {
            return;
        }
        $result = static::$dispatcher->dispatch(new $this->dispatchesEvents[$event]($this));
        if (! is_null($result)) {
            return $result;
        }
    }
troelskn commented 5 years ago

I'm not sure I understand. Wouldn't the saved event cover you?

eporsche commented 5 years ago

Let's say I have an invoice with multiple line items and I want to raise an event when the invoice and all of its line items have been saved to the database. The saved event on the invoice or on the line items will tell me if they have been saved individually but won't tell me if the complete invoice has been saved.