Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.3 groupBy()坏了吗?_Laravel_Eloquent - Fatal编程技术网

Laravel 5.3 groupBy()坏了吗?

Laravel 5.3 groupBy()坏了吗?,laravel,eloquent,Laravel,Eloquent,以下查询适用于Laravel5.2,但不适用于Laravel5.3 $messages = Message::with('sender.profile', 'recipient.profile') ->where('sender_id', 1) ->orWhere('recipient_id', 1) ->groupBy('thread') ->get(); 在5.3中运行时,我收到以下错误消息 SQLSTATE[42000]: Synt

以下查询适用于Laravel5.2,但不适用于Laravel5.3

$messages = Message::with('sender.profile', 'recipient.profile')
    ->where('sender_id', 1)
    ->orWhere('recipient_id', 1)
    ->groupBy('thread')
    ->get();
在5.3中运行时,我收到以下错误消息

SQLSTATE[42000]: Syntax error or access violation: 1055
'tmp_laravel.messages.id' isn't in GROUP BY (SQL: select * from `messages` 
where `sender_id` = 1 or `recipient_id` = 1 group by `thread`)
现在,在您说这是一个数据库问题之前,请记住:同样的查询在Laravel5.2中也适用于相同的数据库。所以,如果这是我的MariaDB版本,它肯定不应该在L5.2中工作,对吗

另外,如果我直接在Sequel Pro中运行查询,它会工作

事实上,为了确保这不是我升级应用程序时做的事情,我安装了两个全新的Laravel版本:5.2和5.3。并在每个版本中安装完全相同的代码,以便它们在控制器、模型、路由、.env等方面完全相同,然后在每个版本中再次运行查询。结果相同:在L5.2中工作,在L5.3中中断。

select*from`messages`
其中`sender\u id`=1
或“收件人id”=1
按线程分组`
此查询无效,但在MySQL/MariaDB中“工作”,并禁用严格模式。Laravel不使用服务器默认设置。您需要修改
config/database.php
并设置

'strict' => false,
在MySQL设置中


不过,您应该考虑重写该查询以明确地选择每个线程的第一个/最后一条消息。

我会采纳您的建议并想出一个新的查询。谢谢你建议重写什么?@timgavin-写一个新问题。这是关于严格的模式和设置。对于MySQL,有很多问题可以回答,比如“如何为每个组选择最新/最早的条目?”。但我还没有看到一个解决方案。如果你在这里发布一个新问题的链接,我稍后会看一看。