csvImporter = $csvImporter; } public function handle(): void { $purge = $this->confirm('Do you want to purge the database of existing students and entries?', false); if ($purge) { Entry::all()->map(function ($entry) { $entry->delete(); }); Student::all()->map(function ($student) { $student->delete(); }); $this->info('Database purged'); } $schools = School::pluck('id', 'name'); $rows = $this->csvImporter->readCsv(storage_path('app/import/import.csv')); $checkedStudents = collect(); foreach ($rows as $row) { $uniqueData = $row['School'].$row['LastName'].$row['LastName']; if ($checkedStudents->contains($uniqueData)) { // continue; } $checkedStudents->push($uniqueData); $currentFirstName = $row['FirstName']; $currentLastName = $row['LastName']; $currentSchoolName = $row['School']; $currentSchoolId = $schools[$currentSchoolName]; if (Student::where('first_name', $currentFirstName)->where('last_name', $currentLastName)->where('school_id', $currentSchoolId)->count() > 0) { $this->info('Student '.$currentFirstName.' '.$currentLastName.' from '.$currentSchoolName.' already exists'); } else { $this->alert('Student '.$currentFirstName.' '.$currentLastName.' from '.$currentSchoolName.' does not exist'); $newStudent = Student::create([ 'school_id' => $currentSchoolId, 'first_name' => $currentFirstName, 'last_name' => $currentLastName, 'grade' => $row['Grade'], ]); $this->info('Student '.$currentFirstName.' '.$currentLastName.' from '.$currentSchoolName.' created with id of: '.$newStudent->id); } } } }