Laravel 5.4-'updateOrCreate'方法检查记录是否已“更新"或“创建" [英] Laravel 5.4 - 'updateOrCreate' method. Check If record was 'updated' or 'created'

查看:391
本文介绍了Laravel 5.4-'updateOrCreate'方法检查记录是否已“更新"或“创建"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用方法updateOrCreate,有什么方法可以知道是更新记录还是创建记录?

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

更新: 我需要使用此功能为创建的记录返回201,对于更新的记录返回204.

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

谢谢.

推荐答案

由于updateOrCreate返回模型实例. https://github.com/laravel /framework/blob/5.4/src/Illuminate/Database/Eloquent/Builder.php#L374

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

您可以使用以下方法检查记录是最近创建的: $instance->wasRecentlyCreated https://github.com/laravel /framework/blob/5.3/src/Illuminate/Database/Eloquent/Model.php#L1593

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

这篇关于Laravel 5.4-'updateOrCreate'方法检查记录是否已“更新"或“创建"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆