首页 > 解决方案 > Laravel Eloquent Group通过抛出 SQL 错误

问题描述

当我手动运行以下 SQL 时,我得到了没有错误的预期结果

select * from `crawl_results` 
    where `user_id` = 1 and `website_id` = 1 
    and `item_state` != 'OK' group by `destination_url` 
    limit 30 offset 0

但是,当我在 Eloquent 中运行它时...

self::where('user_id', Auth::id())
    ->where('website_id', $scanID)
    ->where('item_state', '!=' , 'OK')
    ->groupby('destination_url')->paginate(30)

它会产生此错误:

SQLSTATE[42000]:语法错误或访问冲突:1055 'link_checker.crawl_results.id'不在 GROUP BY 中(SQL:select * from crawl_resultswhere user_id= 1 and website_id= 1 and item_state!= OK group by destination_urllimit 30 offset 0)

不确定抽象背后发生了什么以产生该错误?

标签: phplaravellaravel-5eloquentlaravel-query-builder

解决方案


您应该转到 config\database.php并将strict更改为false

'mysql' => [
   ...
   'strict' => false,
   ...
 ]

推荐阅读