简体   繁体   English

Laravel Eloquent - 关系的 firstOrCreate()

[英]Laravel Eloquent - firstOrCreate() on a relationship

When I try the firstOrCreate() on a relationship of another model, it does not work:当我在另一个模型的关系上尝试firstOrCreate()时,它不起作用:

Client::find($id)->users()->firstOrCreate(array('email' => $email));

This returns an error saying这将返回一个错误说

Call to undefined method Illuminate\\Database\\Query\\Builder::firstOrCreate()调用未定义的方法 Illuminate\\Database\\Query\\Builder::firstOrCreate()

Running this directly on the model User will work though.直接在模型User上运行它会工作。

This won't work, you have to do it manually/directly using User model because users() should return a collection object这行不通,您必须手动/直接使用 User 模型来完成,因为 users() 应该返回一个集合对象

This is correct, the users() function does not return a (new, empty or created) User model, it returns a collection object.这是正确的, users() 函数不返回(新的、空的或创建的)用户模型,它返回一个集合对象。

It's also documented http://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models if you use the laravel relations than it should work.如果您使用 laravel 关系而不是它应该工作,它还记录了http://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models

You are misinterpreting that document.您误解了该文件。 The relationship object has a create() method and a save() method, however the relationship object is not a full model object.关系对象有一个 create() 方法和一个 save() 方法,但是关系对象不是一个完整的模型对象。 To get all of the functionality of a full User model object (such as firstOrCreate()) then you have to call them on the model object.要获得完整用户模型对象(例如 firstOrCreate())的所有功能,您必须在模型对象上调用它们。

So, for example, this code should work:因此,例如,此代码应该可以工作:

$user = User::firstOrNew(array('email' => $email));
Client::find($id)->users()->save($user);

Note that it should be OK to save the $user object here directly from the Client model's relationship, even though it may already exist here, it should just update the client_id field on the $user object when you do so.请注意,从 Client 模型的关系中直接将 $user 对象保存在这里应该是可以的,即使它可能已经存在于此处,但在您这样做时它应该只更新 $user 对象上的 client_id 字段。

这应该做:

Client::find($id)->first()->users()->firstOrCreate(array('email' => $email));

The firstOrCreate method will attempt to locate a database record using the given column / value pairs. firstOrCreate 方法将尝试使用给定的列/值对定位数据库记录。 If the model can not be found in the database如果在数据库中找不到模型

public function firstOrCreate() {
    $requestData = [
        'name' => 'Salma Klocko',
        'email' => 'maida39@frami.biz'
    ];
    $customer = Customer::firstOrCreate($requestData);
    echo "Insert customer detail Successfully with ID : " . $customer->id;
}

You can use updateOrCreate :您可以使用updateOrCreate

$user = App\User::query()->firstWhere('email', 'radical@example.com');

$user->sandwiches()->updateOrCreate(['user_id' => $user->id], [
    'cabbage' => false,
    'bacon' => 9001,
]);

updateOrCreate has two parameters. updateOrCreate有两个参数。 The first one is the match condition(s).第一个是匹配条件。 The second one is the upsert data.第二个是 upsert 数据。 For example, if there is no row with user_id === $user->id , a record will be created with that and the fields from the second parameter.例如,如果没有user_id === $user->id ,则将使用该行和第二个参数中的字段创建记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM