I tried to make an import command using maatwebsite/excel, and in the process, i need two make two new eloquents model, with the default function from maatwebsite import how can i achieve that?
so far i've been doing this
public function model(array $row)
{
return
new Siswa([
'created_at' => $row[1],
'updated_at' => $row[2],
'nis' => $row[3],
'user_id' => $row[4],
'nama' => $row[5],
'nik' => $row[6],
'alamat' => $row[7],
'ayah' => $row[8],
'ibu' => $row[9],
'email' => $row[10],
'kerja_ayah' => $row[11],
'kerja_ibu' => $row[12],
'wali' => $row[13],
'kerja_wali' => $row[14],
'telpon_ayah' => $row[15],
'telpon_ibu' => $row[16],
'kelas' => $row[17],
'referensi' => $row[18],
'note' => $row[19],
'channel' => $row[20],
'url_foto' => $row[21],
'outlet' => $row[22],
'penilaian1' => $row[23],
'penilaian2' => $row[24],
'penilaian3' => $row[25],
'penilaian4' => $row[26],
'penilaian5' => $row[27],
'target' => $row[28],
'prodi_id' => $row[29],
]);
new User([
'name' => $row[5],
'email' => $row[10],
'password' => bcrypt($row[3].'*'),
'outlet_id' => $row[22],
]);
}
}
Here is my controller
public function import(Request $request)
{
Excel::import(new SiswaImport, $request->file('file'));
return redirect()->back();
}
the only problem is that, its only create the first model without the second, what seems to be the problem? can anone help?
The problem is you put return before the new User creation, everything after return will not be executed. so here's how you supposed to modify your Import Code:
public function model(array $row){
new Siswa([
'created_at' => $row[1],
'updated_at' => $row[2],
'nis' => $row[3],
'user_id' => $row[4],
'nama' => $row[5],
'nik' => $row[6],
'alamat' => $row[7],
'ayah' => $row[8],
'ibu' => $row[9],
'email' => $row[10],
'kerja_ayah' => $row[11],
'kerja_ibu' => $row[12],
'wali' => $row[13],
'kerja_wali' => $row[14],
'telpon_ayah' => $row[15],
'telpon_ibu' => $row[16],
'kelas' => $row[17],
'referensi' => $row[18],
'note' => $row[19],
'channel' => $row[20],
'url_foto' => $row[21],
'outlet' => $row[22],
'penilaian1' => $row[23],
'penilaian2' => $row[24],
'penilaian3' => $row[25],
'penilaian4' => $row[26],
'penilaian5' => $row[27],
'target' => $row[28],
'prodi_id' => $row[29],
]);
new User([
'name' => $row[5],
'email' => $row[10],
'password' => bcrypt($row[3].'*'),
'outlet_id' => $row[22],
]);
return 'something';
}