Ignore inserting duplicated data into the DB does not seem to be working properly -- [Question Asked]

Issue

I’m working with Laravel 5.8 and LaravelExcel for importing an Excel file that holds a column named “National Code” and this code must be unique in the Database.

So if a user entered repeated National Codes in his Excel file, a message has to appear and show repeated National Codes to him.

So I coded this at the Import Class:

    $query=DB::table('olympiad_1400');
    
    $repeated = [];
    
    foreach($formatArray as $arr){
        if(!$query->where('mys_ncode',$arr['nationalCode'])->exists()){
           $query->insert([
                'mys_object_id' => $objectId,
                'mys_object_type_id' => $objectTypeId,
                'mys_olp_id' => 4,
                'mys_name' => $arr['name'],
                'mys_fname' => $arr['family'],
                'mys_ncode' => $arr['nationalCode'],
                'mys_paid_price' => $arr['price'],
                'mys_creator_id' => auth()->user()->usr_id,
           ]);
        }else{
           array_push($repeated, $arr['nationalCode']);
        }
    }
    
    if(!empty($repeated)){
        Session::flash('repeated',$repeated);
    }

Now if I test this via uploading an Excel file that contains repeated National Codes which already exists in the DB, it shows the first element in the session $repeated (and ignores inserting it) but surprisingly inserts all the others (however, the are all duplicated).

So how can I fix this and ignore inserting repeated National Codes into the Database?

Answer we found from sources

Try this:

$repeated = [];

foreach($formatArray as $arr){
    $query=DB::table('olympiad_1400');
    if(!$query->where('mys_ncode',$arr['nationalCode'])->exists()){
       $query->insert([
            'mys_object_id' => $objectId,
            'mys_object_type_id' => $objectTypeId,
            'mys_olp_id' => 4,
            'mys_name' => $arr['name'],
            'mys_fname' => $arr['family'],
            'mys_ncode' => $arr['nationalCode'],
            'mys_paid_price' => $arr['price'],
            'mys_creator_id' => auth()->user()->usr_id,
       ]);
    }else{
       array_push($repeated, $arr['nationalCode']);
    }
}

if(!empty($repeated)){
    Session::flash('repeated',$repeated);
}

Answered By – KHIMAJI VALUKIYA

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Posted in PHP

Who we are?

We are team of software engineers in multiple domains like Programming and coding, Fundamentals of computer science, Design and architecture, Algorithms and data structures, Information analysis, Debugging software and Testing software. We are working on Systems developer and application developer. We are curious, methodical, rational, analytical, and logical. Some of us are also conventional, meaning we're conscientious and conservative.

Answer collected from stackoverflow and other sources, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0