csvImporter = $csvImporter; } public function handle(): void { $checkAuditions = $this->confirm('Do you want to check the auditions in the import for validity first?', true); if ($checkAuditions) { $this->call('import:check-auditions'); } $checkSchools = $this->confirm('Do you want to check the schools in the import for validity first?', true); if ($checkSchools) { $this->call('import:check-schools'); } $checkStudents = $this->confirm('Do you want to check the students in the import for validity first?', true); if ($checkStudents) { $this->call('import:check-students'); } $purge = $this->confirm('Do you want to purge the database of existing entries?', false); if ($purge) { Entry::all()->map(function ($entry) { $entry->delete(); }); $this->info('Database purged'); } $schools = School::pluck('id', 'name'); $auditions = Audition::pluck('id', 'name'); $rows = $this->csvImporter->readCsv(storage_path('app/import/import.csv')); foreach ($rows as $row) { $schoolId = $schools[$row['School']]; $student = Student::where('first_name', $row['FirstName'])->where('last_name', $row['LastName'])->where('school_id', $schoolId)->first(); if (! $student) { $this->error('Student '.$row['FirstName'].' '.$row['LastName'].' from '.$row['School'].' does not exist'); return; } $auditionId = $auditions[$row['Instrument']]; try { Entry::create([ 'student_id' => $student->id, 'audition_id' => $auditionId, ]); } catch (\Exception $e) { $this->warn('Entry already exists for student '.$student->full_name().' in audition '.$row['Instrument']); } $this->info('Entry created for student '.$student->full_name().' in audition '.$row['Instrument']); } } }