gpt4 book ai didi

eloquent - Laravel 5 hasManyThrough 数据透视表

转载 作者:行者123 更新时间:2023-12-01 03:40:48 25 4
gpt4 key购买 nike

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

Grade 和 Student 模型都属于另一个

根据我的理解,无法访问需要数据透视表的 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' 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');
});

最佳答案

看看here .
目前 Laravel 5.3 不支持使用数据透视表的 hasManyThrough。只需使用替代方法。
作为替代方式 :

只需考虑这些模型及其关系:

A <=> B with pivot table A_B
B <=> C with pivot table B_C
现在您可以创建一个 枢轴 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)。

关于eloquent - Laravel 5 hasManyThrough 数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31031933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com