ErrorException(E_NOTICE)未定义的偏移量:3 Laravel

时间:2019-12-02 11:35:03

标签: php excel laravel query-builder

在尝试使用查询生成器导入excel时遇到问题(我需要更高的性能)。我想用其相应的转换器转换一些数据,但我不知道哪个引起了未定义的偏移量: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;
    }
}

这是我的Excel数据: Excel Form Data

P.S。我删掉了一些项目以能够发布我的问题

0 个答案:

没有答案