5

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,

2
  • 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? Commented Jan 31, 2017 at 13:10
  • 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 Commented Jan 31, 2017 at 13:37

1 Answer 1

1

No Need to write with.

Article::withCount('Comments')->paginate();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. But unfortunately this didn't worked for me. I tried this but it returns only "comments_count" without comments. I need comments array too.
This will not load the relationship, OP's code is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.