我是laravel的新手,首先我已将表单数据插入数据库中,然后将这些数据提取到数组中。然后,我将此数据插入到新数据库中。每次插入表单数据时,在我的第二个数据库中,所有数据都多次插入。我的代码如下:
foreach($result as $res) {
$res1 = UserList::firstOrCreate($res);
}
这是我的数组。如果数据库中有相同的数据,我只想将数组的新值更新到数据库中
最佳答案
您要使用的方法是updateOrCreate
Model::updateOrCreate(
['needle' => $value],
['field1' => 'value1', 'field2' => 'value2']
);
希望对您有所帮助。