I have the following code in my controller:

for($i=0; $i<$number_of_tourists; $i++) {

$tourist = Tourist::updateOrCreate(['doc_number' => $request['doc_number'][$i]],


each time "updateOrCreate" works, it does 1 of 3 things:

1) updates model instanse OR

2) creates and saves a new one OR

3) leaves everything unchanged (if model with such values already exists).

I need to check if 'updateOrCreate' has done exactly 1 (updated) and then execute some code.

How can I do it?

Thank you in advance!

  • Can you use the updated model event (only fires when the model is actually updated), or should this extra code only be run when this specific set of code updates the model? – patricus Aug 15 '17 at 22:43
  • @patricus, thanx! I've used updated model event, but I managed it only to do some simple things like public function handle(TouristUpdated $event) { dump($event); } (it's code from my listener). I can't figure out how can I instruct listener to add some logic into my controller. The logic actually should be creating an array of all $tourist_to_update 's which have triggered the "updated' event. Hope I explained clearly. – Sergej Fomin Aug 15 '17 at 22:57

In Laravel 5.5 you can finally check if updates have actually been done with the wasChanged method

$tourist = Tourist::updateOrCreate([...]);

// If true then created otherwise maybe updated
$wasCreated = $tourist->wasRecentlyCreated; 

// If true then the row in the database has been modified, 
// otherwise either created or not updated
$wasChanged = $tourist->wasChanged();

if(!$wasCreated && $wasChanged){
  // user existed and there were actually updates taking place.

if($wasCreated && !$wasChanged){
  // user was created. The row wasn't updated after creation.

// If true then the object `$tourist` has been modified but not saved

//You can also check if a specific attribute was changed:

It is pretty easy to determine if the function resulted in an update or an insert (check the wasRecentlyCreated property). However, when using that function, it is less easy to determine if the update actually happened (if the model exists but is not dirty, no update will be performed). I would suggest not using that function, and splitting out the functionality yourself.

This is the function definition:

public function updateOrCreate(array $attributes, array $values = [])
    $instance = $this->firstOrNew($attributes);


    return $instance;

To integrate this into your code, I'd suggest something like:

    $tourist = Tourist::firstOrNew(['doc_number' => $request['doc_number'][$i]]);


    // if the record exists and the fill changed data, update will be performed
    $updated = $tourist->exists && $tourist->isDirty();

    // save the tourist (insert or update)

    if ($updated) {
        // extra code
  • thnx! I've already thought about it, but you expalained it in great details. I still hope there is a chance to use 'updated' model event. But if not - I'll choose this answer as 'right'. – Sergej Fomin Aug 15 '17 at 23:04
  • Could you explain your first sentance It is pretty easy to determine if the function resulted in an update or an insert.? Because thats all I want and I dont know how to get that. – Adam Feb 8 '18 at 17:48
  • NVM I already found it: stackoverflow.com/questions/44238257/… – Adam Feb 8 '18 at 17:52
  • @Adam Glad you found it. I went ahead and updated my answer to mention it, just in case anyone else comes here wondering as well. Thanks. – patricus Feb 8 '18 at 18:22
  • There is now a function called wasChanged() which is doing this job. – Adam Mar 18 '18 at 17:10

@patricus below presented a working way to solve the problem. though @TheFallen here gave a solution which uses Eloquent Events and seems more elegant: Laravel Eloquent Events - implement to save model if Updated

Your Answer

