Hiii! I just trying to using import excel package using maatwebsite . I've succesfully using it horrayyy!! But suddenly I wanna try to import 1 file excel and insert it into two different model. I've tried using one importClass and make a return new model Siswa
and return new model User
like this
(this is importClass what I tried)
namespace App\Imports;
use App\Siswa;
use Maatwebsite\Excel\Concerns\ToModel;
class PengunjungImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Siswa([
'namalengkap' => $row[0],
'kelas' => $row[1],
'jenkel' => $row[2],
'status' => $row[3],
'contact' => $row[4],
]);
return new User([
'name' => $row[0],
'email' => $row[5],
'password' => 'siswasmkn10',
]);
}
}
But it only work on first model (Siswa). Same if I change return new User
to the first and return new Siswa
to the second, it only work on first model (User).
I've tried too import two different importClass in one method like this. But it return error
The file "siswa10duplicate.xls" was not uploaded due to an unknown error.
(this is controller what I tried)
public function processImportSiswa(Request $request)
{
Excel::import(new PengunjungImport, $request->file('import_siswa'));
Excel::import(new UsersImport, $request->file('import_siswa'));
return back();
}
How can I make import one excel file and insert it into multiple table working? Please help me to give some resource or example code it's better :)
You cannot have two followed return on a function because the function will end when the first return is executed. The second approach you tried is the way to go, create one import for each model.
The problem you are having then is that you can't import a file twice in a request, check this issue discussing it. You have to first upload the file and then use that file in both calls.