gpt4 book ai didi

mysql - 拉维尔 : Bulk Insert Other and Ignore Error Exception Duplication

转载 作者:行者123 更新时间:2023-11-30 21:54:25 25 4
gpt4 key购买 nike

在 mysql 中,我在 url 列上设置了唯一键,在 Laravel Controller 中,我创建了数组的键值对以准备批量插入。我想通过忽略错误重复异常来插入,但要为其他人插入。我有以下代码:

Controller .php

try{
$container = array();
foreach ($js->RECORDS as $rec) {
$data = [
'title'=>$rec->title,
'description'=>$rec->description,
'url'=>$rec->url
];
array_push($container, $data);
}
$status = Model::insert($container);
}catch (\Exception $e){
$req->session()->flash('alert-danger', $e->getMessage());
}

最佳答案

try{
foreach ($js->RECORDS as $rec) {
$data = [
'title'=>$rec->title,
'description'=>$rec->description,
'url'=>$rec->url
];
$status = Model::updateOrCreate(['url' => $rec->url], $data);
}
}catch (\Exception $e){
$req->session()->flash('alert-danger', $e->getMessage());
}

关于mysql - 拉维尔 : Bulk Insert Other and Ignore Error Exception Duplication,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45869033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com