简体   繁体   English

Laravel-在phpmyadmin上使用结果的查询分组错误

[英]Laravel - Group By error on query that works on phpmyadmin with results

The query is: 查询是:

$last_topics = Topics::orderby('date', 'DESC')->groupBy('section')->get();

And error: 错误:

在此处输入图片说明

so on error we can see query is: select * from topics group by section order by date desc 等错误,我们可以看到查询: select * from题目group by部分order by日期desc

But as you can see The same query in phpMyAdmin works great, and I have my result 但是如您所见,phpMyAdmin中的相同查询效果很好,我得到了我的结果 在此处输入图片说明

So what's wrong? 那怎么了

I addition I can say that without groupBy in this query inside Laravel doesn't alert error, like: $last_topics = Topics::orderby('date', 'DESC')->get(); 我还可以说,在Laravel内部的此查询中没有groupBy不会警告错误,例如: $last_topics = Topics::orderby('date', 'DESC')->get();

But as I said in phpmyadmin the same query with groupby works great 但是正如我在phpmyadmin中所说的,使用groupby进行相同的查询效果很好

OK this the mode in which your database is being used within your database config /config/database.php . 好的,这是在数据库config /config/database.php使用数据库的方式。 Laravel is not adding all the columns to group by. Laravel不会将所有列都添加为分组依据。

'connections' => [
        'mysql' => [
            // Will work for you
            'strict' => false,

            // wont work for you
            'strict' => true,
        ]
    ]

您可以在get后致电groupBy

Topics::orderby('date', 'DESC')->get()->groupBy('section');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM