I am using https://github.com/Maatwebsite/Laravel-Excel package to import some data from excel. I am using the ToCollection concern with following code
The controller method is:
public function import(Request $request)
{
$this->validate($request, [
'excel' => 'required|mimes:xls,xlsx,csv',
]);
Excel::import(new StudentsImport, request()->file('excel'));
}
The StudentImport class:
class StudentsImport implements ToCollection, WithCalculatedFormulas
{
public function collection(Collection $rows)
{
$english = Subject::find(1);
$nepali = Subject::find(2);
foreach ($rows as $row) {
Student::create([
'roll_no' => $row[0],
'full_name' => $row[1],
'sex' => $row[6],
'father_name' => $row[7],
'mother_name' => $row[8],
'address' => $row[9],
'total_marks_obtained' => $row[87],
'total_percentage' => $row[88],
'grade_points' => $row[90],
'rank' => $row[91],
]);
}
}
}
The loop is workking fine and the code is getting out of the loop, however no Student is being created and i am getting this error: Call to a member function has() on null
PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::cellExists
C:\laragon\www\marksheet\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Worksheet\Worksheet.php:1340