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?
But is there a method that tells me if a record is newly created?
\App\Jobs\AirtableSync::dispatch($record);
in job:
I could pass a value for example $is_newBut is there a method that tells me if a record is newly created?
7 Replies
I would just look to see if the record has an id or not.
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()
If the created_at and updated_at are equal then it is new. Assuming you are using timestamps and it is saved record.
nice... thanks
Sounds like a more appropriate thing to do on a model observer though. To be fair.
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
That’s what I would do.