Created command for importing data from excel
This commit is contained in:
49
app/Imports/HasilBelajarsImport.php
Normal file
49
app/Imports/HasilBelajarsImport.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?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()
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user