如何在查询中加入 2 个集合 laravel

How to join 2 collection in query laravel

我有 StudentEducation 模型,在这个模型中我得到了

public function student_education_countries()
{
    return $this->hasMany(StudentEducationCountry::class, 'education_id');
}

public function student_education_education_types()
{
    return $this->hasMany(StudentEducationEducationType::class, 'education_id');
}

在 StudentEducationCountry 模型中我得到了

public function student_education()
{
    return $this->belongsTo(StudentEducation::class, 'education_id');
}

public function student_country()
{
    return $this->belongsTo(StudentCountry::class, 'country_id');
}

在 StudentEducationEducationType 模型中我得到了

public function student_education()
{
    return $this->belongsTo(StudentEducation::class, 'education_id');
}
public function student_educationcategory()
{
    return $this->belongsTo(StudentEducationcategory::class, 'educationcategory_id');
}

问题是我应该如何参加 country_id 为 2 且类型为次要

的所有教育

使用 whereHas() 很简单

$educations = StudentEducation::query()->whereHas('student_education_countries', function (Builder $query) {
    $query->where('id', 2);
})->whereHas('student_education_education_types', function (Builder $query) {
    //maybe here you need another columns
    $query->where('type', 'secondary');
})->get();