简体   繁体   English

Laravel 通过模型关系

[英]Laravel through model relationships

I have 3 tables with this order:我有 3 个表的顺序如下:

  1. School it has -> id it has -> id学校it has -> id
  2. SchoolSemester it has -> school_id it has -> school_id
  3. SemesterClass it has -> semester_id SemesterClass it has -> semester_id

Now I am trying to make relation between School and SemesterClass in order to get list of classes of each school as well as name of schools in each class.现在我试图在SchoolSemesterClass之间建立关系,以获得每所学校的班级列表以及每班学校的名称。

based on documentation i have this relationships:根据文档,我有这种关系:

school model学校模式

class School extends Model
{
    public function semesters() {
        return $this->hasMany(SchoolSemester::class);
    }

    public function classes() {
        return $this->hasManyThrough(SemesterClass::class, SchoolSemester::class);
    }
}

SchoolSemester model学校学期模式

class SchoolSemester extends Model
{
    public function school() {
        return $this->belongsTo(School::class);
    }

    public function classes() {
        return $this->hasMany(SemesterClass::class, 'semester_id', 'id');
    }
}

SemesterClass model学期班模式

class SemesterClass extends Model
{
    public function school() {
        return $this->hasOneThrough(School::class, SchoolSemester::class, 'school_id', 'id');
    }

    public function semester() {
        return $this->belongsTo(SchoolSemester::class, 'semester_id', 'id');
    }
}

Controller控制器

public function show($id)
{
    $class = SemesterClass::with(['school', 'teacher', 'teacher.user', 'students'])->findOrFail($id);
    dd($class);
    //return view('admin.Classes.show', compact('class'));
}

Results结果

一

Any idea?任何的想法?

Your intermediate table is school_semester , laravel will find the foreign_key school_semester_id by default, however, your foreign_key is semester_id , so you need to specify the foreign_key in hasManyThrough :您的中间表是school_semester ,laravel会发现foreign_key school_semester_id默认情况下,但是,您的foreign_key是semester_id ,所以你需要指定在foreign_key hasManyThrough

    public function classes() {
        return $this->hasManyThrough(
            SemesterClass::class, 
            SchoolSemester::class
            'school_id', // Foreign key on school_semesters table...
            'semester_id', // Foreign key on classes table...
            'id', // Local key on schools table...
            'id' // Local key on school_semesters table...
        );
     }

And change your hasOneThrough code like this:并像这样更改您的hasOneThrough代码:

 public function school() {
        return $this->hasOneThrough(
             School::class, 
             SchoolSemester::class, 
             'id',
             'id',
             'semester_id',
             'school_id' );
    }

Another Solution:另一个解决方案:

Because the reverse is just the concatenation of two BelongsTo relationships, so you can just put the belongsTo in SchoolSemester and School Model.因为反向只是两个 BelongsTo 关系的串联,所以您可以将belongsTo放在belongsTo和 School Model 中。 And get the relationship like this:并得到这样的关系:

SemesterClass::with(['semester.school'])

Or you can define a mutator in SemesterClass Model:或者你可以在 SemesterClass 模型中定义一个 mutator:

protected $appends = ['school'];

public function getSchoolAttribute() {
    return $this->semester->school;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM