0

My excel file consists of student id, name, and course which i'm saving in database. The data was separated by gender in which at row[0]. what i'm trying to achieve here is how can i iterate my data and insert a gender in my database.?

Sample sample.xlsx

enter image description here

//StudentController
Excel::import(new StudentsImport, request()->file('import'));
//ImportSheet
public function model(array $row)
    {
        //empty rows
        if (!isset($row[1]) || $row[0] == '#') {
            return null;
        }

        if (isset($row[1])) {
            return new Student([
                'id_number' => $row[1],
                'name' => $row[2],
                'course' => $row[3],
                'gender' => 'male || female', //<--- HOW CAN I INSERT GENDER HERE BASE ON THE JPEG ABOVE?
            ]);
        }
    }
cupcave
  • 55
  • 9

1 Answers1

0

Here is the way this may help you to get your gender:

public function model(array $row)
{        
    //empty rows
    if (!isset($row[1]) || $row[0] == '#') {
        return null;
    }

    //get the gender
    $gender = ''; // default is empty, you can also assign some default value 
    if (isset($row[0]) && (strtolower($row[0]) == 'male' || strtolower($row[0]) == 'female')) {
        $gender = $row[0];
    }


    if (isset($row[1])) {
        return new Student([
            'id_number' => $row[1],
            'name' => $row[2],
            'course' => $row[3],
            'gender' => $gender
        ]);
    }
}
Sarvil Ajwaliya
  • 233
  • 1
  • 8
  • the return value is still empty. Using the male and female condition will just return `false` because `$row[0] is equal to 1 and 2 and 3`. etc. when i'm importing the data, it reads it per ROW, that's why even if you remove the condition of `male and female` the return value will be the numbers in `row0`. – cupcave Apr 05 '20 at 10:31