I'm using Laravel 5.2 and I'm trying to import an excel file to my database using Laravel excel package. the file contains many sheets, I want to import every sheet to a different table in the database. I have tried the following code but I couldn't find any result.
$file = Input::file('file');
$file_name = $file->getClientOriginalName();
Excel::selectSheets('Branches')->load($file, function($reader){
$reader->each(function($sheet){
foreach($sheet->toArray() as $row){
Branch::firstOrCreate($row=$sheet->toArray());
echo'done';
dd($row);
}
});
});
After trying, searching and going through Laravel Excel documentation, I found that I have to load the whole Excel file and inside this process I can use the sheets selection methods which is given by the package.
Example:
foreach($getSheetName as $sheetName)
{
if ($sheetName === 'Branches')
{
Excel::selectSheets($sheetName)->load($request->file('file'), function ($reader)
{
foreach($reader->toArray() as $sheet)
{
Branch::create($sheet);
}
});
}
}