Laravel 5 hasManyThrough Pivot Table

Laravel 5 hasManyThrough Pivot Table

我正在尝试创建一个关系,以通过一个名为 grade 的模型访问一个名为 comments 的 table,该模型通过该年级的学生加载

Grade 和 Student 模型都 belongToMany of the other

据我了解,无法访问需要枢轴的 hasManyThrough 关系 table(评论没有成绩标识符,只有学生标识符)

class Grade extends Model{
    public function comments(){
        return $this->hasManyThrough("App\Comment","App\Student");
    }
}

我为 Laravel 4 @ HasManyThrough with one-to-many relationship 找到了这些函数,但它给了我错误 Class 'App\Illuminate\Database\Eloquent\Relations\HasMany' not found

我对命名空间没有很好的理解,无法弄清楚我应该在 Laravel 5 的地方做什么。

public function getCommentsAttribute()
{
    if ( ! array_key_exists('comments', $this->relations)) $this->loadComments();

    return $this->getRelation('comments');
}

protected function loadComments()
{
    $comments = Comment::join('grade_student', 'comments.student_id', '=', 'grade_student.student_id')
        ->where('grade_student.grade_id', $this->getKey())
        ->distinct()
        ->get(['comments.*','grade_id']);

    $hasMany = new Illuminate\Database\Eloquent\Relations\HasMany(Translation::query(), $this, 'grade_id', 'id');

    $hasMany->matchMany(array($this), $comments, 'comments');

    return $this;
}

更多信息

评论Table

    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->integer('student_id')->unsigned();
        $table->integer('domain_id')->unsigned();
        $table->integer('teacher_id')->unsigned();
        $table->text('comment');

        $table->foreign('student_id')->references('id')->on('students') ->onDelete('cascade');
        $table->foreign('domain_id') ->references('id')->on('domains')->onDelete('cascade');
        $table->foreign('teacher_id')->references('id')->on('teachers')->onDelete('cascade');
    });

我的学生Table

Schema::create('students', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
    $table->string('unique_identifier');
    $table->string('first_name');
    $table->string('last_name');
    $table->enum('gender',array('m','f'));
});

我的成绩Table

Schema::create('grades', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
    $table->string('name',20);
    $table->integer('school_id')->unsigned();
    $table->integer('level_id')->unsigned();
    $table->foreign('school_id')->references('id')->on('schools')->onDelete('cascade');
    $table->foreign('level_id')->references('id')->on('classes_levels')->onDelete('cascade');
});

我的支点Table

Schema::create('grade_student', function (Blueprint $table) {
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->timestamps();
    $table->integer('grade_id')->unsigned();
    $table->integer('student_id')->unsigned();
    $table->integer('school_id')->unsigned();
    $table->integer('year');

    $table->foreign('grade_id')->references('id')->on('grades')->onDelete('cascade');
    $table->foreign('student_id') ->references('id')->on('students')->onDelete('cascade');
    $table->foreign('school_id')->references('id')->on('schools') ->onDelete('cascade');
});

看看here

目前 Laravel 5.3 不支持 hasManyThrough 使用 pivot table。只需使用替代方法。

另一种方法
只需考虑这些模型及其关系:

A <=> B with pivot table A_B
B <=> C with pivot table B_C

现在您可以创建一个 pivot VIEW 调用 A_C:

SELECT A_id, C_id FROM A_B
INNER JOIN B_C on A_B.B_id = B_C.B_id
GROUP BY A_id, C_id

我想你能猜出我的把戏。
是的,忘了 hasManyThrough。 现在你可以使用 A.belongsToMany(C).