MIF_E31210549/app/Imports/MhsImport.php

55 lines
2.0 KiB
PHP

<?php
namespace App\Imports;
use App\Models\College;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class MhsImport implements ToCollection
{
/**
* @param Collection $collection
*/
public function collection(Collection $collection)
{
// dd($collection);
$index = 1;
foreach ($collection as $row) {
if ($index > 1) {
$check0 = DB::table('mahasiswa')->where('nim', '=', "{$row[0]}")->get();
$check1 = DB::table('jurusan')->where('nama_jurusan', '=', "{$row[2]}")->first();
$id_jur = !empty($check1->id) ? $check1->id : '';
$check2 = DB::table('prodi')->where('nama_prodi', '=', "{$row[3]}")->first();
$id_pro = !empty($check2->id) ? $check2->id : '';
if (count($check0) != 1) {
$data = new College();
$data->nim = !empty($row[0]) ? $row[0] : '';
$data->nama = !empty($row[1]) ? $row[1] : '';
$data->id_jurusan = $id_jur;
$data->id_prodi = $id_pro;
$data->angkatan = !empty($row[4]) ? $row[4] : '';
$data->semester = !empty($row[5]) ? $row[5] : '';
$data->jalur_masuk = !empty($row[6]) ? $row[6] : '';
$data->ukt_sekarang = !empty($row[7]) ? $row[7] : '';
$data->ponsel = !empty($row[8]) ? $row[8] : '';
$data->alamat = !empty($row[9]) ? $row[9] : '';
$data->save();
// $show[] = $id_jur;
// print_r("<pre>");
// print_r($show);
// print_r("</pre>");
// die;
} else {
return redirect('mahasiswa')->with(['error' => 'Ada data mahasiswa yang sama, check data kembali!']);
}
// dd($show);
}
$index++;
}
}
}