Laravel查询生成器的updateOrInsert函数有哪些具体细节?

Mat*_*irt 2 php mysql laravel laravel-5

该功能不在Laravel文档中,我已经在源代码中找到了它,但是我不确定如何使用它。例如,如果我正在使用产品,则要基于其UPC在数据库中插入或更新产品。如果存在具有相同UPC的产品,请使用新值对其进行更新。如果不是,则将新值插入为新产品。使用此功能应如何完成?

谢谢!

Kar*_*ill 5

插入或更新与属性匹配的记录,并用值填充它。

updateOrInsert(array $attributes, array $values = []) 
Run Code Online (Sandbox Code Playgroud)

https://laravel.com/api/master/Illuminate/Database/Query/Builder.html#method_updateOrInsert

DB::table('products')->updateOrInsert(
    [
        'upc' => $request->get('upc'),
    ],
    [
        'upc' => $request->get('upc'),
        'name' => $request->get('name'),
        'vendor' => $request->get('vendor'),
        'description' => $request->get('description')
    ]
);
Run Code Online (Sandbox Code Playgroud)