123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?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'];
- $leaves = Leave::where('student_id', $this['student_id'])->get();
- $leave_days = 0;
- foreach($leaves as $leave) {
- $leave_days += (int) $leave->days;
- }
- $days += $leave_days;
- return Carbon::createFromTimestamp(strtotime($apply_date))->addDays($days)->toDateString();
- }
- }
- }
|