gpt4 book ai didi

laravel - 为什么我的 updateOrInsert 不工作 laravel

转载 作者:行者123 更新时间:2023-12-02 00:08:51 27 4
gpt4 key购买 nike

我使用 updateOrInsert 来避免重复数据,为什么 Update 函数不起作用并总是插入数据?

foreach($datas as $data){
DB::table('users')->updateOrInsert([
'user_connect_id' => $user->connect_id,
'description' => $data['description'],
'created_by' => $login->name,
'modified_by' => $login->name,
'created_at' => Carbon::now(),
]);
}

最佳答案

查看 [updateOrInsert] 此文档 ( https://laravel.com/api/6.x/Illuminate/Database/Query/Builder.html#method_updateOrInsert )。你需要两个参数。一个是匹配属性(即,如果记录存在,您将用来标识记录的属性),另一个是您的数组(您希望插入或更新记录的新值)。

updateOrInsert(array $attributes, array $values = []) 

示例

DB::table('users')->updateOrInsert(
[
'user_connect_id' => $user->connect_id
],
[
'user_connect_id' => $user->connect_id,
'description' => $data['description'],
'created_by' => $login->name,
'modified_by' => $login->name,
'created_at' => Carbon::now(),
]);

关于laravel - 为什么我的 updateOrInsert 不工作 laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59710378/

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