通过 Laravel 中的枢轴 table 与第三个模型的关系

Relationship to a third model through a pivot table in Laravel

我是 Laravel 的初学者,我正在使用以下模式:

Course {
  id,
  name,
  teacher_id
}

Category {
  id,
  name
}

Teacher {
  id,
  name
}

CategoryCourses {
  id,
  course_id,
  category_id
}

如您所见,类别课程是我的支点 table,我在课程和类别之间使用 belongsToMany

现在 - 我想做什么?我正在尝试为类别创建一个 show 视图,我在其中单击该类别,它应该显示该类别的所有课程以及教授相应课程的教师。

因此,例如 - 如果我有 4 个 Laravel 具有“Web 开发”类别的课程 - 我想将所有这些课程显示在一张卡片上,每个课程都有教师的名字。

以下是我的模型:

类别

class Category extends Model
{
    use HasFactory;

    protected $fillable = [
        'name'
    ];

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

    public function teacher()
    {
        return $this->hasOneThrough(Teacher::class, CourseCategory::class);
    }
    
}

课程:

    class Course extends Model
{
    use HasFactory;

    protected $fillable = [
        'name','description','cover','difficulty', 'teacher_id'
    ];

    public function teachers() {
        return $this->belongsTo(Teacher::class, 'teacher_id', 'id');
    }

    public function categories() {
        return $this->belongsToMany(Category::class, 'course_categories');
    }
}

老师:

class Teacher extends Authenticatable
{
    use HasFactory;

    protected $fillable = [
        'name','email','password', 'avatar'
    ];

   public function courses() {
       return $this->hasMany('Course', 'teacher_id');
   }

}

类别控制器-显示($id):

public function show($id)
    {
        $category = Category::findOrFail($id)->first();

        
        $course = Category::with('courses','teacher')->get();
        return view('admin.showcoursesbycategory', compact('category', 'course'));
    }

这是我在编写 $course = Category::with('teacher') 代码后遇到的错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'teachers.course_category_id' in 'on clause' (SQL: select `teachers`.*, `course_categories`.`category_id` as `laravel_through_key` from `teachers` inner join `course_categories` on `course_categories`.`id` = `teachers`.`course_category_id` where `course_categories`.`category_id` in (1, 2, 3, 4))

我如何实现我想要做的事情?我究竟做错了什么?请在这里帮助我。

假设每门课程只有一位教师,按照正常的命名约定,关系应在 Course 模型

上命名为 teacher
class Course extends Model
{
    use HasFactory;

    protected $fillable = [
        'name','description','cover','difficulty', 'teacher_id'
    ];

    public function teacher() {
        return $this->belongsTo(Teacher::class, 'teacher_id', 'id');
    }

    public function categories() {
        return $this->belongsToMany(Category::class, 'course_categories', 'course_id', 'category_id');
    }
}

我想您需要修复 类别 模型

中的关系签名
class Category extends Model
{
    use HasFactory;

    protected $fillable = [
        'name'
    ];

    public function courses()
    {
        /**
         * Change
         * return $this->belongsToMany(Course::class, 'course_categories', 'course_id', 'category_id');
         */
        return $this->belongsToMany(Course::class, 'course_categories', 'category_id', 'course_id');
    }

    public function teacher()
    {
        return $this->hasOneThrough(Teacher::class, CourseCategory::class);
    }
    
}

然后在控制器中你可以将查询写成

public function show($id)
{
    $category = Category::with('courses.teacher')->findOrFail($id);


    return view('admin.showcoursesbycategory', compact('category'));
}

然后在视图中你可以

<h1>{{ $category->name }}</h1>

<p>======== Courses ========</p>

@foreach($category->courses as $course)
    <p>{{ $course->name }}</p>
    <p>Conducted By: {{ $course->teacher->name }}</p>
@endforeach