Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using with and withCount together in laravel eloquent

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,

like image 527
Nadim Aseq A Arman Avatar asked Oct 30 '22 12:10

Nadim Aseq A Arman


1 Answers

No Need to write with.

Article::withCount('Comments')->paginate();
like image 196
Pooja JaJal Avatar answered Nov 10 '22 15:11

Pooja JaJal