Laravel首先创建OR条件

时间:2018-11-23 04:01:01

标签: laravel

可以在firstorcreate函数中使用“或”条件吗?

Model::firstOrCreate(
   ['name' => $request->get('name'), 'oldname' => $request->get('name')]
);

2条件与“ AND”相关,我想使用“ Or” 但不适用于['newname'=> $ request-> get('name')或'oldname'=> $ request-> get('name')]

1 个答案:

答案 0 :(得分:0)

方法firstOrCreate带有一个可选的第二个参数,您可以执行以下操作:

Model::firstOrCreate(
   ['name' => $request->get('name')], ['oldname' => $request->get('name')]
);

来源:https://laravel.com/docs/5.7/eloquent#inserting-and-updating-models

从文档中

// Retrieve flight by name, or create it with the name and delayed attributes...
$flight = App\Flight::firstOrCreate(
    ['name' => 'Flight 10'], ['delayed' => 1]
);