【问题标题】:Maatwebsite Excel 3.1 : how do I skip duplicate data when imported?Maatwebsite Excel 3.1:导入时如何跳过重复数据?
【发布时间】:2019-07-11 08:56:27
【问题描述】:

我在 Laravel Maatwebsite Excel 3.1 上使用 Excel 导入数据。但是当数据库中有相同的数据(主键)时,就会出现错误消息

完整性约束违规:1062 键的重复条目“188281” '初级'

我尝试了解 Maat 网站的文档,但仍然失败

public function storeData(Request $request)
        {
            //VALIDASI
            $this->validate($request, [
                'file' => 'required|mimes:xls,xlsx'
            ]);
        if ($request->hasFile('file')) {
            $file = $request->file('file');

            // dd($file); //GET FILE;
            Excel::import(new MahasiswaImport, $file); //IMPORT FILE
            return redirect('/mahasiswa')->with(['status' => 'Upload success']);
        }
        return redirect('/mahasiswa')->with(['error' => 'Please choose file before']);
    }



<?php

namespace App\Imports;

use App\Mahasiswa;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\Importable;

class MahasiswaImport implements ToModel, WithHeadingRow, WithChunkReading, ShouldQueue

{

  use Importable;
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
    return new Mahasiswa([
      'nim' => $row['nim'],
      'slug' => str_slug($row['nim']),
      'nama_mahasiswa' => $row['nama_mahasiswa'],
      'email' => $row['email'],
      'kode_kelas' => $row['kode_kelas'],
      'alamat' => $row['alamat'],
      'kode_jurusan' => $row['kode_jurusan'],
      'kode_tahun_akademik' => $row['kode_tahun_akademik'],
      'no_hp' => $row['no_hp'],
      'tempat_lahir' => $row['tempat_lahir'],
      // 'tanggal_lahir' => $row['tanggal_lahir'],
      'password' => $row['password']


    ]);
}


public function chunkSize(): int
{
    return 1000;
}

}

【问题讨论】:

  • 你能在你的模型函数中dd($row)并提供它吗?
  • 好的。我已经进入帖子了
  • 你有没有尝试过这样的事情:$data = Mahasiswa::create([ 'nim' =&gt; $row['nim'], 'slug' =&gt; str_slug($row['nim']), 'nama_mahasiswa' =&gt; $row['nama_mahasiswa'], 'email' =&gt; $row['email'], 'kode_kelas' =&gt; $row['kode_kelas'], 'alamat' =&gt; $row['alamat'], 'kode_jurusan' =&gt; $row['kode_jurusan'], 'kode_tahun_akademik' =&gt; $row['kode_tahun_akademik'], 'no_hp' =&gt; $row['no_hp'], 'tempat_lahir' =&gt; $row['tempat_lahir'], 'password' =&gt; $row['password'] ]); return $data;
  • 您的错误来自 sql,因为您的主键有重复键。来自$row 的一些数据是您的主键吗?
  • 是的。 NIM 是主键,。因此,如果数据已经在数据库中,则跳过该数据

标签: php laravel laravel-5 maatwebsite-excel laravel-excel


【解决方案1】:

您需要验证您的row。您可以阅读有关 Row Validation 的文档。

所以你的导入中需要这样的东西:

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

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

或者一些不推荐使用的方式:

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']),
                 ...
                 ]);
       } 
   }
}

【讨论】:

  • sorry,但是出现错误“Class 'App\Imports\Rule' not found”为什么?
  • 在您的导入中添加use Illuminate\Validation\Rule;,让我知道发生了什么。
  • 可以,但是数据没有插入数据库。例如,我在excel中输入了2个数据,1个数据在数据库中,1个数据还不存在。是否可以跳过 Excel 中的现有数据并存储现有数据?
  • 尝试第二种方式,我更新了我的答案。那应该可以。
  • 不客气。也许您可以尝试找到更好的方法,但我认为这很有帮助。祝你好运!
猜你喜欢
  • 2020-04-03
  • 1970-01-01
  • 2020-07-09
  • 2022-11-18
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
相关资源
最近更新 更多