belongsToMany(Course::class, 'sections'); } public function sections() { return $this->hasMany(Section::class); } public function professors() { return $this->belongsToMany(Professor::class)->withPivot('admin_load', 'investigative_load'); } public function getIsFutureAttribute() { return Semester::orderBy('code', 'desc')->take(6)->get()->contains('code', $this->code); } /** * Returns a clone of this semester with the provided code and alpha. * This functions also copies every section and professor_section * * @param string $newSemCode * @param string $newSemAlpha * * @return Semester */ public function clone(string $newSemCode, string $newSemAlpha) { if (Semester::find($newSemCode)) { throw new Exception('A semester with code ' . $newSemCode . ' already exists'); } $newSem = Semester::create([ 'code' => $newSemCode, 'alpha' => $newSemAlpha, ]); // Copy all sections to new semester foreach($this->sections as $section) { $sectionClone = $section->replicate(); $sectionClone->semester_code = $newSem->code; $sectionClone->save(); foreach($section->professors as $professor) { $sectionClone->professors()->attach($professor, ['percent' => $professor->pivot->percent]); } foreach($section->schedules as $schedule) { $sectionClone->schedules()->save($schedule); } } return $newSem; } }