withCount 是 laravel 的一个新方法,用于返回关系的计数。我正在尝试将 with 和 withCount 一起使用。例如:
Article::with('Comments')->withCount('Comments')->paginate();
我面临的问题是,在结果中看到例如:
comments_count = 10
comments = []
它返回 comments 数组 null。我不知道是否有可能同时获得这两个结果。在不同站点的一些文章中,我看到 withCount 仍然有一些限制。但不确定我正在尝试做的事情是否可行。
请帮助我了解这个问题。
谢谢,
最佳答案
不用写了。
Article::withCount('Comments')->paginate();
关于php - 在 laravel eloquent 中一起使用 with 和 withCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41957716/