Laravel 中的 updateOrCreate() 和 updateOrInsert() 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44407210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:05:51  来源:igfitidea点击:

What is the differences between updateOrCreate() and updateOrInsert() in Laravel

phplaravellaravel-5eloquent

提问by Daniel

Both methods seem to create a new record if not existed or update a record with provided data. Whats the difference between the two?

如果不存在,这两种方法似乎都会创建一个新记录,或者使用提供的数据更新记录。两者有什么区别?

回答by Kyslik

updateOrCreateis method of Eloquent Builderand updateOrInsertis method of Query Builder.

updateOrCreate是的方法锋生成器updateOrInsert是的方法查询生成器

updateOrCreatereturns model, whereas updateOrInsertreturns boolean

updateOrCreate返回模型,而updateOrInsert返回布尔值



Signatures from Laravel code:

来自 Laravel 代码的签名:

updateOrCreate

更新或创建

/**
 * Create or update a record matching the attributes, and fill it with values.
 *
 * @param  array  $attributes
 * @param  array  $values
 * @return \Illuminate\Database\Eloquent\Model|static
 */
public function updateOrCreate(array $attributes, array $values = [])

updateOrInsert

更新或插入

/**
 * Insert or update a record matching the attributes, and fill it with values.
 *
 * @param  array  $attributes
 * @param  array  $values
 * @return bool
 */
public function updateOrInsert(array $attributes, array $values = [])