Laravel Updated Event Not Working Properly

245 views Asked by At

Laravel Version: 7.14.1

Here is my Email Model.

 class Email extends Model {

    public static function boot() {
        parent::boot();
        static::updated(function($obj) {
            \Log::info('1-'.$obj->status); 

            if($obj->status==='active') {
                \Log::info('2-'.$obj->status);

                $obj->sendCampaign();
            }
        });
    }

   public function sendCampaign()
    {
        $campaign = $this;

        dispatch(new SendEmailJob($campaign ));

        $campaign->status='sent';
        $campaign->save();

        \Log::info('3-' . $campaign->status);

    }
}

##old status was sent. and I just updated it to active.

And here is my log result:

[2020-09-04 16:47:57] local.INFO: 1-active  
[2020-09-04 16:47:57] local.INFO: 2-active  
[2020-09-04 16:47:58] local.INFO: 1-sent  
[2020-09-04 16:47:58] local.INFO: 3-sent 

I think finally status column should be sent according to my logic and log result.

But when I saw on db it, it was active. I tested this 5 times but the result was same.

Can anyone help me?

1

There are 1 answers

0
Hakim Benmazouz On

i suggest you to write test about it and check if things realy works.

you can use in your test Event::fake() and then check with the function shouldreceive().

i thing this is the best way to do things.