<?php
namespace App\Imports;
use App\Models\KabupatenKota;
use App\Models\Kecamatan;
use App\Models\KelurahanDesa;
use App\Models\Provinsi;
use App\Models\Tps;
use App\Models\User;
use Exception;
use Maatwebsite\Excel\Concerns\ToModel;
class SaksiImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
if(strtolower($row[0]) == "nama"){
return null;
}
$provinsi = Provinsi::where('nama',$row[5])->first();
if(!$provinsi){
throw new Exception("Provinsi Not Found : ".$row[5]);
}
$kabupatenKota = KabupatenKota::where('nama',$row[6])->first();
if(!$kabupatenKota){
throw new Exception("Kabupaten/Kota Not Found : ".$row[6]);
}
$kecamatan = Kecamatan::where('nama',$row[7])->first();
if(!$kecamatan){
throw new Exception("Kecamatan Not Found : ".$row[7]);
}
$kelurahanDesa = KelurahanDesa::where('nama',$row[8])->first();
if(!$kelurahanDesa){
throw new Exception("Kelurahan/Desa Not Found : ".$row[8]);
}
$tps = Tps::where('nomor',(int)$row[9])->whereHas('kelurahan_desas',function($wd)use($kelurahanDesa){
$wd->where('kelurahan_desaables.kelurahan_desa_id',$kelurahanDesa->id);
})->first();
if(!$tps){
throw new Exception("TPS Not Found : ".$row[9]);
}
return new User([
'nama'=>$row[0],
'email'=>$row[1],
'password'=>bcrypt($row[2]),
'no_telp'=>$row[3],
'no_wa'=>$row[4],
'role'=>'saksi',
'provinsi_id'=>$provinsi->id,
'kabupaten_kota_id'=>$kabupatenKota->id,
'kecamatan_id'=>$kecamatan->id,
'kelurahan_desa_id'=>$kelurahanDesa->id,
'tps_id'=>$tps->id,
]);
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]