| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Models;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Model;
- class StudentCourse extends Model
- {
- protected $table = 'student_courses';
- protected $guarded = [];
- public function course()
- {
- return $this->hasOne('App\Models\Course', 'id', 'course_id');
- }
- public function getTeacherNames()
- {
- if($this['assign_teacher'] == 1) {
- return '全部';
- }
- $ids = $this['studentCourseTeachers']->pluck('teacher_id');
- return (new Teacher())->whereIn('id', $ids)->get()->implode('name', ',');
- }
- public function getTeacherFullNames()
- {
- if($this['assign_teacher'] == 1) {
- return '全部';
- }
- $ids = $this['studentCourseTeachers']->pluck('teacher_id');
- return (new Teacher())->whereIn('id', $ids)->get()->implode('name', '/');
- }
- public function studentCourseTeachers()
- {
- return $this->hasMany('App\Models\StudentCourseTeacher');
- }
- public function getTeacherIds()
- {
- if($this['assign_teacher'] == 1) {
- return collect();
- }
- return $this['studentCourseTeachers']->pluck('teacher_id');
- }
- public function updateStudentCourseTeachers($teachers, $assign_teacher)
- {
- StudentCourseTeacher::where('student_course_id', $this['id'])->delete();
- if($assign_teacher == 2) {
- if(!empty($teachers) && is_array($teachers)) {
- foreach($teachers as $teacher) {
- StudentCourseTeacher::create([
- 'student_course_id' => $this['id'],
- 'teacher_id' => $teacher,
- 'student_id' => $this['student_id']
- ]);
- }
- }
- } else {
- StudentCourseTeacher::create([
- 'student_course_id' => $this['id'],
- 'teacher_id' => 0,
- 'student_id' => $this['student_id']
- ]);
- }
- }
- public function computeEndDate()
- {
- $apply_date = $this['apply_date'];
- if(empty($apply_date) || empty($this['duration'])) {
- return '';
- } else {
- $days = $this['duration'];
- $leave_days = (int)Leave::where('student_id', $this['id'])->get()->count('days');
- $days += $leave_days;
- return Carbon::createFromTimestamp(strtotime($apply_date))->addDays($days)->toDateString();
- }
- }
- }
|