dikplhd/app/Imports/DatasetTable1Import.php

52 lines
1.5 KiB
PHP

<?php
namespace App\Imports;
use App\Models\Dataset\DatasetTable1;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
class DatasetTable1Import implements ToModel,WithStartRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
protected $datasetId;
protected $userId;
public function __construct($datasetId,$userId)
{
$this->datasetId = $datasetId;
$this->userId = $userId;
}
public function startRow(): int
{
return 6;
}
public function model(array $row)
{
// cek kalau row kosong jangan insert
if ($row[0] === null && $row[1] === null && $row[2] === null && $row[3] === null && $row[4] === null && $row[5] === null && $row[6] === null && $row[7] === null && $row[8] === null) {
return null; // baris kosong dilewati
}
return new DatasetTable1([
'dataset_id' => $this->datasetId,
'created_by' => $this->userId,
'nama_kawasan_1' => $row[0],
'nama_kawasan_2' => $row[1],
'nama_kawasan_3' => $row[2],
'nama_kawasan_4' => $row[3],
'luas_kawasan' => $row[4],
'tutupan_lahan_vegetasi' => $row[5],
'tutupan_lahan_area_terbangun' => $row[6],
'tutupan_lahan_tanah_terbuka' => $row[7],
'tutupan_lahan_badan_air' => $row[8],
]);
}
}