Php 在laravel中使用with和with count

Php 在laravel中使用with和with count,php,laravel,laravel-5,eloquent,with-statement,Php,Laravel,Laravel 5,Eloquent,With Statement,withCount是laravel返回关系计数的一种新方法。我试着用with和withCount。例如: Article::with('Comments')->withCount('Comments')->paginate(); 我面临的问题是,在结果中,例如: comments_count = 10 comments = [] 它返回注释数组null。我不知道是否可能同时得到这两个结果。在不同网站的一些文章中,我看到withCount仍然有一些限制。但我不确定我想做的是可能的还

withCount是laravel返回关系计数的一种新方法。我试着用with和withCount。例如:

Article::with('Comments')->withCount('Comments')->paginate();
我面临的问题是,在结果中,例如:

comments_count = 10
comments = []
它返回注释数组null。我不知道是否可能同时得到这两个结果。在不同网站的一些文章中,我看到withCount仍然有一些限制。但我不确定我想做的是可能的还是不可能的

请帮助我了解这个问题


谢谢你,

不用写信了

Article::withCount('Comments')->paginate();

谢谢你的回复。但不幸的是,这对我不起作用。我试过了,但是它只返回“comments\u count”,没有注释。我也需要注释数组。这将不会加载关系,OP的代码是正确的。是。这两种结果都有可能得到。你的代码可以做到这一点。你能显示那个查询的结果吗?如果我只使用“WITH”,它返回:comments:[{id:1,comments:'-“},{id:2,comments:'-“}],如果我使用online WITH count,它返回:comments\u count:2。。。。如果我同时使用这两种方法,它将返回:comments:[],comments\u count:2