Laravel 5拥有许多透视表

时间:2015-06-24 16:24:18

标签: eloquent laravel-5 relationship

我正在尝试创建一个关系,通过名为grade的模型访问一个名为comments的表,通过成绩中的学生加载

等级和学生模型都属于另一个

的其他人

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

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'找不到

我对命名空间没有很好的理解,也无法解决我在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;
}

更多信息

评论表

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

我的学生表

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

我的成绩表

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

我的数据透视表

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

1 个答案:

答案 0 :(得分:2)

看看here

目前Laravel 5.3并不支持使用数据透视表的hasManyThrough。只需使用其他方法。

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

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)。
只是不要忘记标记为已接受的答案:))