Laravel 5 hasManyThrough 数据透视表

Posted

技术标签:

【中文标题】Laravel 5 hasManyThrough 数据透视表【英文标题】:Laravel 5 hasManyThrough Pivot Table 【发布时间】:2015-09-10 23:21:06 【问题描述】:

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

Grade 和 Student 模型都属于另一个模型

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

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;

更多信息

评论表

    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】:

看看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)。

【讨论】:

以上是关于Laravel 5 hasManyThrough 数据透视表的主要内容,如果未能解决你的问题,请参考以下文章

hasManyThrough - Laravel 5.8

Laravel 关系:hasManyThrough、belongsTo、belongsToMany

通过 Laravel 关系查询 HasManyThrough 检索相关记录

Laravel:如何平均嵌套 hasMany 关系(hasManyThrough)

Laravel hasManyThrough 深入

Laravel Eloquent: hasManyThrough 多对多关系