我的项目包括:
关系:
说明:
通过搜索,我发现: 接下来的两张图片..这些关系正确吗?
我的工作
:-迁移:
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';
}
警告:我无法在(注册和教学)中建立关系。
我的请求:
等待帮助,
答案 0 :(得分:0)
这显然没有经过测试,但是应该可以帮助您找到解决问题的方法。
表名
模型
课程模型
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();
}
}