Laravel 5 hasManyThrough 数据透视表

Mic*_*nes 5 relationship eloquent laravel-5

我正在尝试创建一种关系,以通过名为成绩的模型访问名为评论的表,该模型通过成绩中的学生加载

Grade 和 Student 模型都属于另一个

根据我的理解,无法访问需要数据透视表的 hasManyThrough 关系(评论没有成绩标识符,只有学生标识符)

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

我为 Laravel 4 @ HasManyThrough找到了这些具有一对多关系的函数,但它给了我错误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;
}
Run Code Online (Sandbox Code Playgroud)

更多信息

评论表

    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');
    });
Run Code Online (Sandbox Code Playgroud)

我的学生桌

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'));
});
Run Code Online (Sandbox Code Playgroud)

我的成绩表

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');
});
Run Code Online (Sandbox Code Playgroud)

我的数据透视表

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');
});
Run Code Online (Sandbox Code Playgroud)

Kha*_*leh 4

看看这里

目前 Laravel 5.3 不支持使用数据透视表的 hasManyThrough。只需使用替代方法即可。

作为替代方法
只需考虑这些模型及其关系:

A <=> B with pivot table A_B
B <=> C with pivot table B_C
Run Code Online (Sandbox Code Playgroud)

现在您可以创建一个数据透视表 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
Run Code Online (Sandbox Code Playgroud)

我想你能猜到我的伎俩。
是的,忘记 hasManyThrough。现在您可以使用 A.belongsToMany(C)。