1

I'm currently rebuilding an application and I currently have this in observer

public function updated(Lead $lead) {
    
    if ( $lead->isDirty('status') && $lead->status === 'rejected') {
        LeadRejected::dispatch($lead);
    }
        
}

I'm removing the use of observer and want to dispatch the event directly from the model

something like;

protected $dispatchesEvents = [
    'updated' => LeadUpdated::class,
];

I'd like to know if its possible to create a custom event key (model's lifecycle) I can hook the LeadRejected event

something like

protected $dispatchesEvents = [
    'rejected' => LeadRejected::class,
];

1 Answer 1

0

There is something similar in Laravel which helps you achieve your goal. Here is an example:

app\Model\Lead.php

protected $observables = ['rejected'];

protected static function booted()
{
    static::updated(function ($model) {
         $this->fireModelEvent('rejected', false);
    });
}

app\Observers\LeadObserver:

public function rejected(Lead $lead)
{
   //do something.
}

Ref: https://www.itsolutionstuff.com/post/how-to-create-custom-model-events-in-laravelexample.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.