$row) { if($key == 0 || empty($row[0])) continue; $student = Student::firstOrCreate([ 'name' => $row[0], 'phone' => $row[1] ], [ 'remark' => $row[6] ]); if(empty($student)) continue; $course = Course::firstOrCreate([ 'name' => $row[2] ]); if(empty($course)) continue; $assign_teacher = $row[5] == '全部' ? 1 : 2; $duration = null; if(!empty($row[3]) && !empty($row[4]) && $row[4] > $row[3]) { $duration = Carbon::createFromTimestamp(strtotime($row[3]))->diffInDays(Carbon::createFromTimestamp(strtotime($row[4]))); } $student_course = StudentCourse::firstOrCreate([ 'student_id' => $student->id, 'course_id' => $course->id ], [ 'apply_date' => $row[3], 'duration' => $duration, 'assign_teacher' => $assign_teacher ]); if(empty($student_course)) continue; if($assign_teacher == 2) { $teacher_names = explode(',', $row[5]); foreach($teacher_names as $teacher_name) { $teacher = Teacher::firstOrCreate([ 'name' => $teacher_name ]); StudentCourseTeacher::firstOrCreate([ 'student_course_id' => $student_course->id, 'teacher_id' => $teacher->id, 'student_id' => $student->id, ]); } } } } }