50 lines
1.1 KiB
PHP
50 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Siswa;
|
|
use App\HasilBelajar;
|
|
use Maatwebsite\Excel\Concerns\ToModel;
|
|
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
|
|
class HasilBelajarsImport implements ToModel, WithHeadingRow
|
|
{
|
|
/**
|
|
* @param array $row
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
*/
|
|
public function model(array $row)
|
|
{
|
|
$siswa = Siswa::where('nisn', $row['nisn'])
|
|
->where('nis', $row['nis'])->first();
|
|
|
|
if (!$siswa)
|
|
return null;
|
|
|
|
$fieldMeta = [];
|
|
foreach ($row as $key => $value) {
|
|
if (preg_match("/\_+/", $key)) {
|
|
$rowNames = explode('_', $key);
|
|
$fieldMeta[$rowNames[0]][$rowNames[1]][$rowNames[2]] = round($value, 2);
|
|
}
|
|
else {
|
|
$fieldMeta[$key] = $value;
|
|
}
|
|
}
|
|
|
|
return new HasilBelajar([
|
|
'siswa_id' => $siswa->id,
|
|
'meta' => $fieldMeta,
|
|
]);
|
|
}
|
|
|
|
public function sheets(): array
|
|
{
|
|
return [
|
|
new FirstSheetImport()
|
|
];
|
|
}
|
|
}
|