Reputation: 6276
I am developing an application in Laravel 5.6
I'm having a simple table with following columns:
company_id project_id company_role_id company_specialisation_id
And this represents the model AssociateCompanies
, which has relation of company
, project
, role
, specialisation
now I am having some query to get the attribute:
$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');
I want to collect all unique fields with their counts from two columns company_id
and specialisation_id
, but groupBy
is not giving me proper the results so I am unbale to proceed further:
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() fromproject_associate_company
wherecompanies
.id
=project_associate_company
.company_id
andproject_associate_company
.deleted_at
is null) asassociated_projects_count
fromcompanies
whereproject_associate_company
.company_id
=companies
.id
and exists (select * fromprojects
inner joinproject_owner_relation
onprojects
.id
=project_owner_relation
.project_id
wherecompanies
.id
=project_owner_relation
.company_id
andslug
= lodha-patel-estate-tower-a-b-mumbai andprojects
.deleted_at
is null) andcompanies
.deleted_at
is null) andproject_associate_company
.deleted_at
is null group bycompany_id
,company_specialisation_id
)"
So I tried running raw queries like this:
$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 fromproject_associate_company
where exists (select , (select count() fromproject_associate_company
wherecompanies
.id
=project_associate_company
.company_id
andproject_associate_company
.deleted_at
is null) asassociated_projects_count
fromcompanies
whereproject_associate_company
.company_id
=companies
.id
and exists (select * fromprojects
inner joinproject_owner_relation
onprojects
.id
=project_owner_relation
.project_id
wherecompanies
.id
=project_owner_relation
.company_id
andslug
= lodha-patel-estate-tower-a-b-mumbai andprojects
.deleted_at
is null) andcompanies
.deleted_at
is null) andproject_associate_company
.deleted_at
is null)"
Suggest me better way to get this. Thanks.
Upvotes: 0
Views: 525
Reputation: 908
Initially I want to notice you can not "group by" inside the "select" statement.
So you can not aggregate the non-aggregated columns. This means is grouped fields can be has got multiple "role" so you can not "load" the "roles". The query should be like the one of the follows:
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');
Or :
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');
My suggestions are like this but I think you can solve it with raw MySQL queries rather than use to Eloquent.
Upvotes: 1