0

I'm exporting an excel file to a database using Laravel excel

class StudentsImport implements ToModel
{  

public function model(array $row)

{
return new Students([
    'student_name'          => $row[0],
    'student_email'         => $row[1],
 }
}

and it works fine, I however also want to pass a universal variable that is not in the excel file. In this case, the school_id is available in the controller but I can't figure out how to pass it to the imports class.

   public function model(array $row)

 {
   return new Students([
'student_name'          => $row[0],
'student_email'         => $row[1],
'school_id'             => $school_id,
}

Help will be appreciated. Thanks.

user3714932
  • 1,253
  • 2
  • 16
  • 29

1 Answers1

0

You can pass the variable to the constructor and then in your imported class you can access like this

 public function  __construct($school_id)
{ $this->school_id= $school_id; }
---------------------------------
Singh
  • 1