withCount()不包括已删除的行?

编程入门 行业动态 更新时间:2024-10-27 02:24:57
本文介绍了withCount()不包括已删除的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使withCount('comments')也包括所有已删除/已整理的行?

How can I make withCount('comments') also include all deleted/trashed rows?

例如,如果我有5条注释,而我删除了1条,我仍然希望withCount('comments')返回5条,但返回4条.

For example, if I have 5 comments, and I delete 1, I still expect withCount('comments') to return 5, but instead it's returning 4.

我的完整查询看起来像这样:

My full query looks something like this:

$query = Post::withTrashed() ->withCount('comments') ->get();

推荐答案

我认为您可以尝试

$query = Post::withCount('comments') ->withTrashed() ->get();

OR

$query = DB::table('post') ->select('comments', DB::raw('count(*) as comments')) ->get();

希望这项工作对您有用!

Hope this work for you!

更多推荐

withCount()不包括已删除的行?

本文发布于:2023-10-24 09:52:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不包括   withCount

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!