I'm having problem when trying to import excel with query builder (I need more performance). I want to convert some of the data with it's corresponding converters, but I don't know which one caused Undefined offset: 3...
public function collection(Collection $rows)
{
foreach ($rows as $row) {
// convert data
$idrow[0] = $this->convert2($this->convert($row[0])); // no_pendaftaran
$idrow[6] = $this->convert3($this->convert($row[6])); // kode_pilihan_1
$idrow[7] = $this->convert3($this->convert($row[7])); // kode_pilihan_2
// add new data to database if not already exist
DB::table('data_prodi')->insertOrIgnore(['kode_prodi' => $idrow[6],'prodi' => $row[6]]);
DB::table('data_prodi')->insertOrIgnore(['kode_prodi' => $idrow[7],'prodi' => $row[7]]);
// add data to main table
DB::table('data_train')->insert([
'no_pendaftaran' => $idrow[0],
'kode_pilihan_1' => $idrow[6],
'kode_pilihan_2' => $idrow[7],
'daftar_kembali' => $row[10],
]);
}
}
// if null then -
private function convert($data) {
if ($data === null) {
return '-';
} else {
return $data;
}
}
// fill no_pendaftaran with random number if -
private function convert2($data) {
if ($data === '-') {
$id = mt_rand(1000000000,9999999999);
$ifExists = DB::table('data_train')->where('no_pendaftaran','=',$id);
if ($ifExists->count() > 0) {
return convert2($data);
}
return $id;
} else {
return $data;
}
}
// replace non-numeric data with it's corresponding id in database if found, if not make it's id starting from 1
private function convert3($data) {
if (preg_match("/[^0-9-]/i", $data)){
$ifExists = DB::table('data_prodi')->select('kode_prodi')->where('prodi','=',$data)->first();
if ($ifExists){
$data = (int)implode((array)$ifExists);
} else {
@$id++;
$data = $id;
}
return $data;
} else {
return $data;
}
}
And here is my excel data: Excel Form Data
P.S. I cut out some items to be able to post my question