gpt4 book ai didi

php - 如何在 laravel 中设置 withCount 条件

转载 作者:行者123 更新时间:2023-11-29 09:24:35 27 4
gpt4 key购买 nike

我有一个users型号和 comments现在我要选择users关系超过100 comments为此,我正在这样做:

$users = User::withCount('comments')->having('comments_count','>','100')->paginate(10);
return $users;

但是在执行代码时出现以下错误:

Undefined column: 7 ERROR: column "comments_count" does not exist LINE 1:

现在我的问题是如何让用户获得超过 100 条评论或任何其他关系,例如 likes谢谢

最佳答案

您可以将闭包传递给 withCount() 方法,但这只会影响对哪些行进行计数。所以你不能这样做。

您可以使用 has() 方法。

$users = User::has('comments', '>', 100)->withCount('comments')->paginate(10);

关于php - 如何在 laravel 中设置 withCount 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59820184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com