5
votes

withCount is a new method of laravel to return count of the relations. I'm trying to use with and withCount together. For example:

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

Problem I'm facing is, in the results is see for example:

comments_count = 10
comments = []

It returns comments array null. I don't know is it possible to get both the results or not. In some articles on different sites, i see withCount still has some restrictions. But not sure what I'm trying to do is possible or not.

Please help me to know about this issue.

Thank you,

1
yes. it is possible to get both results. and your code is fine to do that. can you show the result of that query? - Sanzeeb Aryal
if I use only "WITH" it returns: comments:[{id:1, comments:"---"},{id:2, comments:"-----"}], if I use online withCount, it returns: comments_count:2.... and if I use both it returns: comments:[], comments_count:2 - Nadim Aseq A Arman

1 Answers

2
votes

No Need to write with.

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