繁体   English   中英

为什么我的 updateOrInsert 在 laravel 9x 中不起作用

[英]Why my updateOrInsert doesn't work in laravel 9x

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

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Wip;
use App\Models\db2;

class WipController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $tgl_skrg = date('Y-m-d 00:00:00');
        $tgl_skrg2 = date('Y-m-d 23:00:00');
        $nilai = '00017';
       
        $db2s = DB::connection('mysql2')->table("wip_tracking_1")->get();
        
        $data = json_decode($db2s, true);

        foreach ($data as $db2 ){
           DB::table('wip_tracking')
            ->updateOrInsert(
                [
                "TP" =>$db2 ['TP'],
                "TP_code"=>$db2 ['TP_code'],
                "VIN"=>$db2 ['VIN'] ,
                "Flag"=>$db2 ['Flag'] ,
                "Suffix"=>$db2 ['Suffix'] ,
                "Color_Code"=>$db2 ['Color_Code'] ,
                "Scan_date"=>$db2 ['Scan_date'] ,
                "Production_date"=>$db2 ['Production_date'] ,
                "Production_shift"=>$db2 ['Production_shift'] ,
                "Model_code"=>$db2 ['Model_code'] ,
                "Brand"=>$db2 ['Brand'] ,
                "Destination_code"=>$db2 ['Destination_code'] ,
                "WIP_code"=>$db2 ['WIP_code'] ,
                "ADM_Production_ID"=>$db2 ['ADM_Production_ID'] ,
                "Plan_delivery_date"=>$db2 ['Plan_delivery_date'] ,
                "created_at" => date('Y-m-d H:i:s'),
                ],
                [
                    "ADM_Production_ID"=> $db2['ADM_Production_ID']
                ],
                [
                    "VIN"=> $db2['VIN']
                ],

            );
            
          
        } 
                    
 
    }

updateOrInsert 应该接收两个参数 arrays。第一个是条件数组,它将检查要更新或插入的记录和第二个列和值数组。

例如

DB::table('users')
    ->updateOrInsert(
        ['email' => 'john@example.com', 'name' => 'John'],
        ['votes' => '2']
    );

第一个数组['email' => 'john@example.com', 'name' => 'John']将查找匹配的列 => 值对。

第二个数组['votes' => '2']将根据调查结果更新或插入数据。

https://laravel.com/docs/9.x/queries#update-or-insert

在您的情况下,您似乎正在尝试匹配第一个数组中的很多值,这可能是为什么从未找到完全匹配并且它总是产生一个新条目的原因,请尝试限制并保持您的条件数组更多具体的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM