Tuesday, March 21, 2017

Laravel query builder, having issue

i have an issue, and don't know how fix it. I need to get an specific object from select such as { "iso":"UA", "stories":122, "title":Ukraine }

so i have an sql query

SELECT
c.iso,
locale.title as title,
(SELECT COUNT(DISTINCT s.id) FROM stories AS s WHERE s.country_id = c.id) AS stories
FROM `countries` AS c
LEFT JOIN countries_locale AS c_l ON c.id=c_l.country_id
LEFT JOIN locales AS locale ON c_l.locale_id=locale.id
WHERE locale.locale = 'en'
GROUP BY c.id
HAVING stories>0

and it works fine, so i try to rewrite this query to Laravel QB:

DB::table($this->getTable())
->select(
'countries.iso as iso',
'locales.title as title',
DB::raw('(SELECT COUNT(s.id) FROM stories AS s WHERE  s.country_id = countries.id) AS stories')
)
->leftJoin('countries_locale', 'countries.id', '=', 'countries_locale.country_id')
->leftJoin('locales', 'countries_locale.locale_id', '=', 'locales.id')
->where('locales.locale', \App::getLocale())
->groupBy('iso')
->having('stories', '>', 0)
->get();

And then i get an error

Syntax error or access violation: 1055 'way.countries.id' isn't in GROUP BY

and in shows me an sql string, that i can successfully exec in mysql



via mikrafizik

Advertisement