laravel 首先或创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25178464/
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-08 16:40:29  来源:igfitidea点击:

First Or Create

laravellaravel-4eloquent

提问by panthro

I know using:

我知道使用:

User::firstOrCreate(array('name' => $input['name'], 'email' => $input['email'], 'password' => $input['password']));

Checks whether the user exists first, if not it creates it, but how does it check? Does it check on all the params provided or is there a way to specifiy a specific param, e.g. can I just check that the email address exists, and not the name - as two users may have the same name but their email address needs to be unique.

首先检查用户是否存在,如果不存在则创建它,但它如何检查?它是否检查提供的所有参数,或者有没有办法指定特定参数,例如我可以检查电子邮件地址是否存在,而不是名称 - 因为两个用户可能具有相同的名称,但他们的电子邮件地址需要是独特的。

回答by lowerends

firstOrCreate()checks for all the arguments to be present before it findsa match. If not all arguments match, then a new instance of the model will be created.

firstOrCreate()finds匹配之前检查是否存在所有参数。如果并非所有参数都匹配,则将创建模型的新实例。

If you only want to check on a specific field, then use firstOrCreate(['field_name' => 'value'])with only one item in the array. This will return the first item that matches, or create a new one if not matches are found.

如果您只想检查特定字段,则firstOrCreate(['field_name' => 'value'])仅使用数组中的一项。这将返回匹配的第一个项目,如果未找到匹配项,则创建一个新项目。

The difference between firstOrCreate()and firstOrNew():

之间的差异firstOrCreate()firstOrNew()

  • firstOrCreate()will automatically create a new entry in the database if there is not match found. Otherwise it will give you the matched item.
  • firstOrNew()will give you a new model instance to work with if not match was found, but will only be saved to the database when you explicitly do so (calling save()on the model). Otherwise it will give you the matched item.
  • firstOrCreate()如果没有找到匹配项,将自动在数据库中创建一个新条目。否则它会给你匹配的项目。
  • firstOrNew()如果发现不匹配,将为您提供一个新的模型实例,但只有在您明确这样做(调用save()模型)时才会将其保存到数据库中。否则它会给你匹配的项目。

Choosing between one or the other depends on what you want to do. If you want to modify the model instance before it is saved for the first time (e.g. setting a nameor some mandatory field), you should use firstOrNew(). If you can just use the arguments to immediately create a new model instance in the database without modifying it, you can use firstOrCreate().

在一个或另一个之间进行选择取决于您想要做什么。如果您想在第一次保存模型实例之前对其进行修改(例如设置 aname或某些必填字段),您应该使用firstOrNew(). 如果您可以使用参数立即在数据库中创建一个新的模型实例而无需修改它,则可以使用firstOrCreate().

回答by umbrel

Previous answer is obsolete. It's possible to achieve in one step since Laravel 5.3, firstOrCreatenow has second parameter values, which is being used for new record, but not for search

以前的答案已过时。从 Laravel 5.3 开始可以一步实现,firstOrCreate现在有第二个参数values,用于新记录,但不用于搜索

$user = User::firstOrCreate([
    'email' => '[email protected]'
], [
    'firstName' => 'Taylor',
    'lastName' => 'Otwell'
]);

回答by A_funs

An update:

更新:

As of Laravel 5.3 doing this in a single step is possible; the firstOrCreatemethod now accepts an optional second array as an argument.

从 Laravel 5.3 开始,可以一步完成;该firstOrCreate方法现在接受一个可选的第二个数组作为参数。

The first array argument is the array on which the fields/values are matched, and the second array is the additional fields to use in the creation of the model if no match is found via matching the fields/values in the first array:

第一个数组参数是匹配字段/值的数组,如果通过匹配第一个数组中的字段/值未找到匹配项,则第二个数组是用于创建模型的附加字段:

See documentation

查看文档

回答by albus_severus

firstOrCreate() checks for all the arguments to be present before it finds a match.

firstOrCreate() 在找到匹配项之前检查是否存在所有参数。

If you only want to check on a specific field, then use firstOrCreate(['field_name' => 'value']) like

如果您只想检查特定字段,请使用 firstOrCreate(['field_name' => 'value']) 像

$user = User::firstOrCreate([
    'email' => '[email protected]'
], [
    'firstName' => 'abcd',
    'lastName' => 'efgh',
    'veristyName'=>'xyz',
]);

Then it check only the email

然后它只检查电子邮件