Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 - 'updateOrCreate' method. Check If record was 'updated' or 'created'

Using the method updateOrCreate, is there a way to know which if a record was updated or created?

UPDATE: I need to use this feature to return 201, for created record, or 204 for updated record.

Thank you.

like image 703
vlauciani Avatar asked May 29 '17 08:05

vlauciani


1 Answers

Since updateOrCreate returns the model instance. https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Builder.php#L374

You can check that a record was recently created using: $instance->wasRecentlyCreated https://github.com/laravel/framework/blob/5.3/src/Illuminate/Database/Eloquent/Model.php#L1593

like image 95
Oluwafemi Sule Avatar answered Nov 17 '22 04:11

Oluwafemi Sule