Laravel 5.3:选择leftjion不能与groupBy一起使用

时间:2016-10-19 14:48:38

标签: laravel count group-by

我想计算所有类别项目列表旁边的帖子数量如下:

  • 第1类(25)
  • 第2类(12)
  • 类别3(8)
  • 类别......(67)

在Laravel 5.2中,我使用下面的代码来实现上述结果:

$cateCount = Category::leftjoin('posts', 'posts.icategoryid', '=', 'categories.id')
          ->groupBy('categories.id')
          ->get(['categories.id', 'categories.name', DB::raw('count(posts.icategoryid) as mycount')]);

但是,我升级到Laravel 5.3,它显示以下错误:

  

SQLSTATE [42000]:语法错误或访问冲突:1055' posts.categories.name'不在GROUP BY(SQL:选择categoriesidcategoriesname,将(posts.icategoryid)计为来自categories的mycount左侧加入posts postsicategoryid = categoriesid分组categoriesid

你能告诉我如何解决这个或更好的方法来实现上述结果吗?

谢谢,Vannak

1 个答案:

答案 0 :(得分:0)

有一个类似的错误,在我的情况下,它与MySQL严格模式有关,将在5.3中启用:

https://mattstauffer.co/blog/strict-mode-and-other-mysql-customizations-in-laravel-5-2

'connections' => [
    'mysql' => [
        // Behave like MySQL 5.6
        'strict' => false,

        // Behave like MySQL 5.7
        'strict' => true,
    ]
]