laravel中的学生,老师和课程关系

时间:2018-06-20 20:57:08

标签: php database laravel eloquent relationship

我在Laravel项目中的关系有问题:


我的项目包括:

  • 学生(身份证名-手机-电子邮件)
  • 老师(ID-名称-手机-电子邮件)
  • 课程(ID-名称-说明)

关系:

  • 每门课程都由一位或多位老师提供。
  • 每位老师提供一门或多门课程。
  • 每个学生都注册了一门或多门课程。
  • 每个学生在一位老师的陪同下参加一门或多门课程

说明:

  • 课程和老师=(多对多)。
  • 课程和学生=(多对多)。
  • 学生和老师=(多对多)。

通过搜索,我发现: 接下来的两张图片..这些关系正确吗?



我的工作

-迁移:

Schema::create('courses', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('name');
            $table->text('description');

            $table->timestamps();
        });


Schema::create('students', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('name');
            $table->string('mobile');
            $table->string('email');

            $table->timestamps();
        });


Schema::create('teachers', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('name');
            $table->string('mobile');
            $table->string('email');

            $table->timestamps();
        });


Schema::create('enrollments', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->integer('course_id')->unsigned();
            $table->integer('student_id')->unsigned();

            $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
            $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');

            $table->timestamps();
        });


Schema::create('teaches', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('course_id')->unsigned();
            $table->integer('teacher_id')->unsigned();

            $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
            $table->foreign('teacher_id')->references('id')->on('teachers')->onDelete('cascade');

            $table->timestamps();
        });


Schema::create('student_course_teacher', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('enroll_student_id')->unsigned();
            $table->integer('enroll_course_id')->unsigned();
            $table->integer('teach_teacher_id')->unsigned();

            $table->foreign('enroll_student_id')->references('student_id')->on('enrollments')->onDelete('cascade');
            $table->foreign('enroll_course_id')->references('course_id')->on('enrollments')->onDelete('cascade');
            $table->foreign('teach_teacher_id')->references('teacher_id')->on('teaches')->onDelete('cascade');

            $table->timestamps();
        });

-型号:

-课程:

class Course extends Model
{
    protected $table = 'courses';

    public function students()
    {
        return $this->belongsToMany(Student::class, 'enrollments', 'student_id', 'course_id')->withTimestamps();
    }

    public function teachers()
    {
        return $this->belongsToMany(Teacher::class, 'teaches', 'teacher_id', 'course_id')->withTimestamps();
    }
}

-学生:

class Student extends Model
{
    protected $table = 'students';

    public function courses()
    {
        return $this->belongsToMany(Course::class, 'enrollments', 'course_id', 'student_id')->withTimestamps();
    }
}

-老师:

class Teacher extends Model
{
    protected $table = 'teachers';

    public function courses()
    {
        return $this->belongsToMany(Course::class, 'teaches', 'course_id', 'teacher_id')->withTimestamps();
    }
}

-注册:

class Enrollment extends Model
{
    protected $table = 'enrollments';

}

-示教:

class Teach extends Model
{
    protected $table = 'teaches';

}

警告:我无法在(注册教学)中建立关系。

我的请求:

  • 完整的关系
  • 我想通过老师的信息吸引学生参加某门课程。
  • 我想和她的学生和老师一起上课。
  • 我想和学生一起上某些老师的课程。

等待帮助,

1 个答案:

答案 0 :(得分:0)

这显然没有经过测试,但是应该可以帮助您找到解决问题的方法。

表名

  1. 课程(用于课程模型)
  2. 学生(针对学生模型)
  3. 老师(针对教师模型)
  4. course_student(学生与课程之间的多对多关系)-为此,您将创建一个额外的模型(CourseStudent),该模型实际上将用于第6点。
  5. course_teacher(对于教师与课程之间的多对多关系)
  6. course_student_teacher(对于CourseStudent和Teacher之间的多对多关系)

模型

课程模型

class Course extends Model
{
  public function students()
  {
    return $this->belongsToMany(Student::class)->withTimestamps();
  }

  public function teachers()
  {
    return $this->belongsToMany(Teacher::class)->withTimestamps();
  }
}

学生模型

class Student extends Model
{
  public function courses()
  {
    return $this->belongsToMany(Course::class)->withTimestamps();
  }
}

教师模型

class Teacher extends Model
{
  public function courses()
  {
    return $this->belongsToMany(Course::class)->withTimestamps();
  }

  // Courses/Students Currently Being Taught
  public function courseStudents()
  {
    return $this->belongsToMany(CourseStudent::class, 'course_student_teacher')
                ->withTimestamps();
  }

  // List Of Students Currently Taught For Different Courses
  public function allStudentsCurrentlyTaught()
  {
    return $this->courseStudents()->get()->map(function($courseStudent) {
      return $courseStudent->student;
    })->unique('id');
  }

  // List Of Courses Currently Taught To Different Students
  public function coursesCurrentlyTaughtToStudents()
  {
    return $this->courseStudents()->get()->map(function($courseStudent) {
      return $courseStudent->course;
    })->unique('id');
  }

  // List Of Courses Taught To A Particular Students
  public function coursesTaughtToAStudent($studentId)
  {
    return $this->courseStudents()->get()->where('student_id', $studentId)->map(function($courseStudent) {
      return $courseStudent->course;
    });
  }

  // List Of Students Taught For A Particular Course
  public function coursesTaughtForACourse($courseId)
  {
    return $this->courseStudents()->get()->where('course_id', $courseId)->map(function($courseStudent) {
      return $courseStudent->course;
    });
  }
}

课程学生模型,用于创建数据透视表course_student_teacher

class CourseStudent extends Model
{
  protected $table = 'course_student';

  public function course()
  {
    return $this->belongsTo(Course::class)->withTimestamps();
  }

  public function student()
  {
    return $this->belongsTo(Student::class)->withTimestamps();
  }

  public function teachers()
  {
    return $this->belongsToMany(Teacher::class, 'course_student_teacher')->withTimestamps();
  }
}