Maatwebsite Excel 3.1 : 如何在导入时跳过重复数据?
Maatwebsite Excel 3.1 : how do I skip duplicate data when imported?
我在 Laravel Maatwebsite Excel 3.1 上使用 Excel 导入数据。但是当数据库中有相同的数据(主键)时,就会出现错误消息
integrity constraint violation: 1062 Duplicate entry '188281' for key
'PRIMARY'
我试图理解 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;
}
}
您需要验证您的 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']),
...
]);
}
}
}
使用 Maat 网站 Excel 3.1;导入时如何跳过重复数据?您可以像这样检查数据库中是否有任何列已经具有该值。
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
}
我在 Laravel Maatwebsite Excel 3.1 上使用 Excel 导入数据。但是当数据库中有相同的数据(主键)时,就会出现错误消息
integrity constraint violation: 1062 Duplicate entry '188281' for key 'PRIMARY'
我试图理解 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;
}
}
您需要验证您的 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']),
...
]);
}
}
}
使用 Maat 网站 Excel 3.1;导入时如何跳过重复数据?您可以像这样检查数据库中是否有任何列已经具有该值。
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
}