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

只谈情不闲聊 提交于 2019-12-22 04:58:18

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/44238257/laravel-5-4-updateorcreate-method-check-if-record-was-updated-or-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!