Laravel 查询生成器,有问题

Laravel query builder, having issue

我有一个问题,不知道如何解决。 我需要从 select 中获取特定对象,例如 { "iso":"UA", "stories":122, "title":乌克兰 }

所以我有一个 sql 查询

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

它工作正常,所以我尝试将此查询重写为 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();

然后我得到一个错误

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

并向我展示了一个 sql 字符串,我可以在 mysql

中成功执行

如果你想让laravel接受不是很严格的查询,使用

'strict' => false

在数据库配置中。 或者,在您的情况下,您可以将这两列都放在分组依据中的 select 中。