将原始查询添加到 eloquent 关系

Adding raw query to eloquent relationship

我正在 Laravel 5.6 中开发一个应用程序 我有一个简单的 table 包含以下列:

company_id    project_id    company_role_id    company_specialisation_id

这表示模型 AssociateCompanies,它具有 companyprojectrolespecialisation 的关系,现在我有一些查询要获取属性:

$companies = AssociateCompany::whereHas('company', function ($q) use ($request) {
    $q->whereHas('projectOwners', function ($q) use($request) {
        $q->where('slug', $request->slug);
    });
})->groupBy('company_id', 'company_specialisation_id')->with('company', 'role', 'specialisation');

我想从 company_idspecialisation_id 两列中收集所有唯一字段及其计数,但是 groupBy 没有给我正确的结果,所以我无法继续进行:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'conxn.project_associate_company.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from project_associate_company where exists (select , (select count() from project_associate_company where companies.id = project_associate_company.company_id and project_associate_company.deleted_at is null) as associated_projects_count from companies where project_associate_company.company_id = companies.id and exists (select * from projects inner join project_owner_relation on projects.id = project_owner_relation.project_id where companies.id = project_owner_relation.company_id and slug = lodha-patel-estate-tower-a-b-mumbai and projects.deleted_at is null) and companies.deleted_at is null) and project_associate_company.deleted_at is null group by company_id, company_specialisation_id)"

所以我尝试了 运行 这样的原始查询:

$companies = AssociateCompany::whereHas('company', function ($q) use ($request) {
        $q->whereHas('projectOwners', function ($q) use($request) {
            $q->where('slug', $request->slug);
        });
    })->selectRaw(DB::raw('COUNT(*) AS count GROUP BY company_id , company_specialisation_id'))
    ->with('company', 'companyRole', 'specialisation')->get();

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY company_id , company_specialisation_id from project_associate_company' at line 1 (SQL: select COUNT(*) AS count GROUP BY company_id , company_specialisation_id from project_associate_company where exists (select , (select count() from project_associate_company where companies.id = project_associate_company.company_id and project_associate_company.deleted_at is null) as associated_projects_count from companies where project_associate_company.company_id = companies.id and exists (select * from projects inner join project_owner_relation on projects.id = project_owner_relation.project_id where companies.id = project_owner_relation.company_id and slug = lodha-patel-estate-tower-a-b-mumbai and projects.deleted_at is null) and companies.deleted_at is null) and project_associate_company.deleted_at is null)"

建议我获得它的更好方法。谢谢。

最初我想注意你不能在 "select" 语句中 "group by" 。

所以你不能聚合非聚合列。这意味着分组字段可以有多个 "role" 所以你不能 "load" "roles"。查询应类似于以下之一:

AssociateCompany::whereHas('company', function ($q) use ($request) {
    $q->whereHas('projectOwners', function ($q) use($request) {
        $q->where('slug', $request->slug);
    });
})->select('company_id', 'company_specialisation_id', \DB::raw('COUNT(*) as cnt'))
->groupBy('company_id', 'company_specialisation_id')
->with('company', 'specialisation');

或:

AssociateCompany::whereHas('company', function ($q) use ($request) {
    $q->whereHas('projectOwners', function ($q) use($request) {
        $q->where('slug', $request->slug);
    });
})->select('company_id', 'company_specialisation_id', 'company_role_id', \DB::raw('COUNT(*) as cnt'))
->groupBy('company_id', 'company_specialisation_id', 'company_role_id')
->with('company', 'specialisation', 'role');

我的建议是这样的,但我认为您可以使用原始 MySQL 查询来解决它,而不是使用 Eloquent.