Cara Mengatasi Error Validasi Import Excel | Laravel
- by Asep Ependi
- 30 December 2022
- 905 views

Selamat datang kembali di website asepit yang selalu memberikan informasi mengenai dunia Teknologi khususnya pada bidang Komputer, Pemrograman, dan juga Jaringan. Bagaimana kabar kalian semua mudah - mudahan dalam keadaan sehat wal afiat dan selalu dilancarkan rezekinya, pada kesempatan kali ini saya akan membahas mengenai cara mengatasi error validasi pada saat import Excel di laravel. Pada kasus ini sebenarnya data yang dibutuhkan itu tersedia namun row yang kosong mungkin ikut terimport makanya terjadi kesalahan kurang lebih seperti ini
Import Class Model
<?php
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use App\Anggota;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithValidation;
use App\Simpanan;
use App\SimpananDetail;
class SimpananDebet implements ToCollection, WithStartRow, WithValidation
{
protected $periode;
protected $anggota;
public function __construct($periode)
{
$this->periode = $periode;
}
public function startRow(): int
{
return 2;
}
public function collection(Collection $rows)
{
foreach ($rows as $row) {
$last_record = Simpanan::select()->orderBy('id', 'DESC')->first();
if ($last_record) {
$code = str_replace('SMPN-', '', $last_record->code);
$no = ltrim($code, "0") + 1;
$substr = substr_replace($code, "", -strlen($no));
$codesimpanan = ('SMPN-') . '' . $substr . $no;
} else {
$codesimpanan = 'SMPN-001';
}
$this->anggota = Anggota::select()->where('nik', '=', $row[0])->value('id');
$simpanan = Simpanan::create([
'code' => $codesimpanan,
'periode_id' => $this->periode->id,
'anggota_id' => $this->anggota,
'user_id' => auth()->user()->id,
'tgl' => date('Y-m-d', strtotime($row[1])),
'jenis_pembayaran' => $row[2],
'jenis_transaksi' => 'debet',
'keterangan' => $row[3],
]);
$simpananPokok = DB::table('barang_jasas')
->select('barang_jasas.id as id', 'barang_jasas.name as name', 'barang_jasas.harga_jual as harga_jual')
->leftJoin('type_barangs', 'barang_jasas.type_barang_id', '=', 'type_barangs.id')
->where('type_barangs.code', 'J001')
->first();
$simpananWajib = DB::table('barang_jasas')
->select('barang_jasas.id as id', 'barang_jasas.name as name', 'barang_jasas.harga_jual as harga_jual')
->leftJoin('type_barangs', 'barang_jasas.type_barang_id', '=', 'type_barangs.id')
->where('type_barangs.code', 'J002')
->first();
$simpananSukarela = DB::table('barang_jasas')
->select('barang_jasas.id as id', 'barang_jasas.name as name', 'barang_jasas.harga_jual as harga_jual')
->leftJoin('type_barangs', 'barang_jasas.type_barang_id', '=', 'type_barangs.id')
->where('type_barangs.code', 'J003')
->first();
for ($i=0; $i <= 3 ; $i++) {
// simpanan pokok
if ($i === 1) {
SimpananDetail::create([
'simpanan_id' => $simpanan->id,
'barang_jasa_id' => $simpananPokok->id,
'debet' => $row[4]
]);
}
// simpanan wajib
if ($i === 2) {
SimpananDetail::create([
'simpanan_id' => $simpanan->id,
'barang_jasa_id' => $simpananWajib->id,
'debet' => $row[5]
]);
}
// simpanan Sukarela
if ($i === 3) {
SimpananDetail::create([
'simpanan_id' => $simpanan->id,
'barang_jasa_id' => $simpananSukarela->id,
'debet' => $row[6]
]);
}
}
}
public function rules(): array
{
return [
'0' => 'required',
'*.0' => 'required',
'1' => 'required',
'*.1' => 'required',
'2' => 'required',
'*.2' => 'required',
'3' => 'required',
'*.3' => 'required',
'4' => 'required',
'*.4' => 'required',
'5' => 'required',
'*.5' => 'required',
'6' => 'required',
'*.6' => 'required',
];
}
}
}
Controller Method
public function import(Request $request)
{
$this->validate($request, [
'file' => 'required'
]);
$periode = Periode::where('status', '1')->first();
// import data
try {
Excel::import(new SimpananDebet($periode), request()->file('file'));
activity()->log('Upload Data Simpanan Debet');
return redirect()->route('simpanan-debet.index')->with('success', __('Data Simpanan Debet Telah Sukses Di Tambahkan'));
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
$failures = $e->failures();
foreach ($failures as $failure) {
$failure->row(); // row that went wrong
$failure->attribute(); // either heading key (if using heading row concern) or column index
$failure->errors(); // Actual error messages from Laravel validator
foreach($failure->errors() as $key)
{
return redirect()->route('simpanan-debet.index')->with('error', $key);
}
$failure->values(); // The values of the row that has failed.
}
}
}
Berikut Data yang di Import
Dan berikut error yang dihasilkan
Kita bisa mengatasi nya dengan sedikit menambahkan atribut class di Import Class Model dengan "SkipsEmptyRows" kurang lebih akan menjadi seperti ini
<?php
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use App\Anggota;
use App\Helpers\Tanggal;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\WithValidation;
use App\Simpanan;
use App\SimpananDetail;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
class SimpananDebet implements ToCollection, WithStartRow, WithValidation, SkipsEmptyRows
{
protected $periode;
protected $anggota;
public function __construct($periode)
{
$this->periode = $periode;
}
public function startRow(): int
{
return 2;
}
public function collection(Collection $rows)
{
foreach ($rows as $row) {
$last_record = Simpanan::select()->orderBy('id', 'DESC')->first();
if ($last_record) {
$code = str_replace('SMPN-', '', $last_record->code);
$no = ltrim($code, "0") + 1;
$substr = substr_replace($code, "", -strlen($no));
$codesimpanan = ('SMPN-') . '' . $substr . $no;
} else {
$codesimpanan = 'SMPN-001';
}
$this->anggota = Anggota::select()->where('nik', '=', $row[0])->value('id');
$simpanan = Simpanan::create([
'code' => $codesimpanan,
'periode_id' => $this->periode->id,
'anggota_id' => $this->anggota,
'user_id' => auth()->user()->id,
'tgl' => date('Y-m-d', strtotime($row[1])),
'jenis_pembayaran' => $row[2],
'jenis_transaksi' => 'debet',
'keterangan' => $row[3],
]);
$simpananPokok = DB::table('barang_jasas')
->select('barang_jasas.id as id', 'barang_jasas.name as name', 'barang_jasas.harga_jual as harga_jual')
->leftJoin('type_barangs', 'barang_jasas.type_barang_id', '=', 'type_barangs.id')
->where('type_barangs.code', 'J001')
->first();
$simpananWajib = DB::table('barang_jasas')
->select('barang_jasas.id as id', 'barang_jasas.name as name', 'barang_jasas.harga_jual as harga_jual')
->leftJoin('type_barangs', 'barang_jasas.type_barang_id', '=', 'type_barangs.id')
->where('type_barangs.code', 'J002')
->first();
$simpananSukarela = DB::table('barang_jasas')
->select('barang_jasas.id as id', 'barang_jasas.name as name', 'barang_jasas.harga_jual as harga_jual')
->leftJoin('type_barangs', 'barang_jasas.type_barang_id', '=', 'type_barangs.id')
->where('type_barangs.code', 'J003')
->first();
for ($i=0; $i <= 3 ; $i++) {
// simpanan pokok
if ($i === 1) {
SimpananDetail::create([
'simpanan_id' => $simpanan->id,
'barang_jasa_id' => $simpananPokok->id,
'debet' => $row[4]
]);
}
// simpanan wajib
if ($i === 2) {
SimpananDetail::create([
'simpanan_id' => $simpanan->id,
'barang_jasa_id' => $simpananWajib->id,
'debet' => $row[5]
]);
}
// simpanan Sukarela
if ($i === 3) {
SimpananDetail::create([
'simpanan_id' => $simpanan->id,
'barang_jasa_id' => $simpananSukarela->id,
'debet' => $row[6]
]);
}
}
}
}
public function rules(): array
{
return [
'0' => 'required',
'*.0' => 'required',
'1' => 'required',
'*.1' => 'required',
'2' => 'required',
'*.2' => 'required',
'3' => 'required',
'*.3' => 'required',
'4' => 'required',
'*.4' => 'required',
'5' => 'required',
'*.5' => 'required',
'6' => 'required',
'*.6' => 'required',
];
}
}
Berhasil.....
Mungkin hanya itu untuk pembahasan kali ini, saya mohon ma'af bila dalam penyampaian serta penulisan terdapat sebuah kesalahan. Jangan lupa ikuti juga platform ASEP IT lainnya dibawah ini, saya cukupkan sekian dan Terima Kasih...
Website klik disini
Youtube klik disini
Fans Page klik disini
Comments