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,
];
SymmetricsWeb
  • 586
  • 6
  • 20

1 Answers1

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

free2idol1
  • 174
  • 1
  • 3
  • 12