猿问

Laravel Eloquent GroupBy抛出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:选择* from crawl_resultswhere user_id= 1 and website_id= 1 and item_state!= OK group by destination_urllimit 30 offset 0)


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


一只斗牛犬
浏览 233回答 2
2回答

SMILET

将查询更改为查询构建器查询DB::table('crawl_results')->where('user_id', Auth::id())->where('website_id', $scanID)->where('item_state', '!=' , 'OK')->groupby('destination_url')->paginate(30);它应该工作正常。在文档中提到了GROUP BY无法在分页中有效执行当前,使用Laravel无法有效地执行使用groupBy语句的分页操作。如果需要使用带有分页结果集的groupBy,建议您查询数据库并手动创建分页器。

斯蒂芬大帝

您应该转到 config \ database.php并将strict更改为false'mysql' => [   ...   'strict' => false,   ... ]
随时随地看视频慕课网APP
我要回答