Maatwebsite Excel 3.1 : how do I skip duplicate data when imported?



PHP Snippet 1:

public function rules(): array
{
    return [
        'nim' => Rule::unique('mahasiswa', 'nim'), // Table name, field in your db
    ];
}

public function customValidationMessages()
{
    return [
        'nim.unique' => 'Custom message',
    ];
}

PHP Snippet 2:

public function model(array $data)
{
    foreach($data as $row) {
       $data = Mahasiswa::find($row['nim']);
       if (empty($data)) {
          return new Mahasiswa([
                 'nim' => $row['nim'],
                 'slug' => str_slug($row['nim']),
                 ...
                 ]);
       } 
   }
}

PHP Snippet 3:

public function model(array $row)
{
    $bin = DB::table('bin_info')->get();

    // Get all bin number from the $bin collection
    $bin_number = $bin->pluck('bin_number');
    
    // Checking if the bin number is already in the database
    if ($bin_number->contains($row[0]) == false) 
    {
        return new Bin([
            'bin_number' => $this->binNumberCheck($row[0]),
            'type' => $this->typeCheck($row[1]),
            'product' => $row[2],
            'category' => $row[3],
            'bank' => $row[4],
        ]);
    }
    else null; // if the bin number is already in the database, return null
}