【问题标题】:student and teacher and course relationships in laravellaravel 中的学生和老师以及课程关系
【发布时间】:2018-06-20 20:57:07
【问题描述】:

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


我的项目包括:

  • 学生(ID-姓名-手机-电子邮件)
  • 教师(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';

}

注意:我无法在(Enrollment & Teach)中建立关系。

我的要求:

  • 完整的关系
  • 我想为某门课程的学生提供教师信息。
  • 我想和她的学生和老师一起上课。
  • 我想与学生一起参加某些教师课程。

等待帮助,

【问题讨论】:

  • 看来你甚至没有尝试过解决这个问题:laravel.com/docs/5.6/eloquent-relationships
  • @Devon:再次检查问题..我已经编写了我的代码
  • 不确定您的问题是什么。您不需要用于教学和注册的模型,因为它们是数据透视表。
  • @Devon :你能纠正关系吗?好吧,我想处理我在上一个问题中提到的查询
  • 不...因为您还没有说明它们有什么问题。 SO 不是用于代码审查,而是针对特定问题和答案。 *.com/help/mcve

标签: php database laravel eloquent relationship


【解决方案1】:

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

表格名称

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

模型

课程模式

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;
    });
  }
}

CourseStudent 模型用于创建数据透视表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();
  }
}

【讨论】:

  • 谢谢,但我不知道如何使用:in (Teacher Model) => students, coursesCurrentlyTaughtToStudents, coursesTaughtToAStudent,coursesTaughtForACourse,你能给我他们的使用例子吗..请
  • prateekkathal : 你能给我举个例子供他们使用吗..拜托
  • @netonline 据我了解,您的项目将与 StudentCourse & Teacher 建立 多对多 关系。因此,这些函数将进入course_student_teacher 表(这是 StudentCourse 和教师之间的关系表),然后收集函数将帮助您过滤掉记录以获取诸如“该老师当前教授的学生”或“该课程的课程”之类的内容目前正在由老师授课”。请尝试使用 Laravel 约定创建表格,然后测试这些关系。之后你只会得到更好的理解。谢谢。 :)
最近更新 更多