第一或创造

我知道使用:

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

首先检查用户是否存在(如果不是它创建的话) ,但它如何检查呢?它是检查所有提供的参数,还是有一种方法来指定一个特定的参数,例如,我可以只检查电子邮件地址存在,而不是名称-因为两个用户可能有相同的名称,但他们的电子邮件地址需要是唯一的。

140653 次浏览

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

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.

The difference between firstOrCreate() and 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.

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 name or 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().

As of Laravel 5.3 it's possible to do this in one step with firstOrCreate using a second optional values parameter used only if a new record is created, and not for the initial search. It's explained in the documentation as follows:

The firstOrCreate method will attempt to locate a database record using the given column / value pairs. If the model cannot be found in the database, a record will be inserted with the attributes resulting from merging the first array argument with the optional second array argument.

Example

$user = User::firstOrCreate([
'email' => 'dummy@domain.example'
], [
'firstName' => 'Taylor',
'lastName' => 'Otwell'
]);

This returns the User for the specified email if found, otherwise creates and returns a new user with the combined array of email, firstName, and lastName.


This technique requires Mass Assignment to be set up, either using the fillable or guarded properties to dictate which fields may be passed into the create call.

For this example the following would work (as a property of the User class):

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['email', 'firstName', 'lastName'];

An update:

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

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 the Laravel API documentation

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

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

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

Then it checks only the email.

You can always check if in current instance the record is created with the help of

$user->wasRecentlyCreated


So basically you can


if($user->wasRecentlyCreated){


// do what you need to do here


}