dikplhd/app/Imports/DatasetTable39Import.php

48 lines
1.2 KiB
PHP

<?php
namespace App\Imports;
use App\Models\Dataset\DatasetTable39;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
class DatasetTable39Import 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) {
return null; // baris kosong dilewati
}
return new DatasetTable39([
'dataset_id' => $this->datasetId,
'created_by' => $this->userId,
'kendaraan' => @$row[1] ? $row[1] : 0,
'jumlah' => @$row[2] ? $row[2] : 0,
'bensin' => @$row[3] ? $row[3] : 0,
'solar' => @$row[4] ? $row[4] : 0,
'gas' => @$row[5] ? $row[5] : 0,
]);
}
}