withCount()不包括已删除的行? [英] withCount() doesn't include deleted rows?

查看:70
本文介绍了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()不包括已删除的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆