When dispatching a job and passing a model ... Is model beibng create or Update

I am passing the $record to the job but how do I know if it is a new record or an update? \App\Jobs\AirtableSync::dispatch($record); in job:
public function __construct( public Lead $record )
{
dd($record);
}
public function __construct( public Lead $record )
{
dd($record);
}
I could pass a value for example $is_new
But is there a method that tells me if a record is newly created?
7 Replies
X7Ryan
X7Ryan2w ago
I would just look to see if the record has an id or not.
ddoddsr
ddoddsrOP2w ago
Since both of these are ->after() the action, the record is saved and both return the id I can just pass along a flag for $is_new in EditAction and CreateAction after()
awcodes
awcodes2w ago
If the created_at and updated_at are equal then it is new. Assuming you are using timestamps and it is saved record.
ddoddsr
ddoddsrOP2w ago
nice... thanks
awcodes
awcodes2w ago
Sounds like a more appropriate thing to do on a model observer though. To be fair.
ddoddsr
ddoddsrOP2w ago
I was wondering. That would be better if I turn it off when importing with ->saveQuietly() but on for everything else. So for the sake of discussion, I'll dispatch events on created, updated. and in an event listeners I'll make the approate business logic and api calls
awcodes
awcodes2w ago
That’s what I would do.

Did you find this page helpful?