Get relation count Laravel JSON

berkayk

I have a Post model where has many comments via hasMany('App/Comment') relation. I am trying to return the number of comments in JSON format. The response I would like to get is like

comments: [...],
numComments: 5,
...

I am able to return the comments but I don't know whether there is an easy way to get number of comments with eager loading.

Post::whereIn('user_id', $ids)->with('comments')->get();

Thanks.

berkayk

Just found the solution here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel relation total count

From Dev

Laravel get relation count where a field in the database is set to a certain value

From Dev

Laravel: get models if count of relation has some condition

From Dev

Use select count in relation Laravel

From Dev

laravel 5.1: get relation in with

From Dev

Laravel: get single model with a relation

From Dev

Laravel user get relation tables

From Dev

Laravel get nested relation value

From Dev

Get relation for multiple objects with laravel

From Dev

count query using eloquent relation laravel

From Dev

Laravel: Order query by count id field on relation

From Dev

How to get count of relation field in parse

From Dev

count query - how to get relation instead of hash

From Dev

Laravel get Eloquent relation with default value if relation is not present?

From Dev

Laravel get data from many to many relation

From Dev

laravel get relation hasMany model with where clause

From Dev

Octobercms/Laravel get id based on relation

From Dev

Laravel get a model collection ordered by a relation column

From Dev

Laravel collection get occurrences of a MtoM relation

From Dev

laravel get relation hasMany model with where clause

From Dev

laravel how to get all nested relation to a collection

From Dev

Laravel hasMany relation count number of likes and comments on post

From Dev

Get full json struct in a belongsTo relation

From Dev

Postgres : get min and max rows count in many to many relation table

From Dev

how to get data with relation in Laravel trying to get Bill Products to view

From Dev

Laravel: Get pivot data for specific many to many relation

From Dev

How to get list depending on condition over the relation in Laravel

From Dev

Laravel Trying to get property of non-object One to One relation

From Dev

Laravel : Error in creating a relation to get first child of each parent entity

Related Related

  1. 1

    Laravel relation total count

  2. 2

    Laravel get relation count where a field in the database is set to a certain value

  3. 3

    Laravel: get models if count of relation has some condition

  4. 4

    Use select count in relation Laravel

  5. 5

    laravel 5.1: get relation in with

  6. 6

    Laravel: get single model with a relation

  7. 7

    Laravel user get relation tables

  8. 8

    Laravel get nested relation value

  9. 9

    Get relation for multiple objects with laravel

  10. 10

    count query using eloquent relation laravel

  11. 11

    Laravel: Order query by count id field on relation

  12. 12

    How to get count of relation field in parse

  13. 13

    count query - how to get relation instead of hash

  14. 14

    Laravel get Eloquent relation with default value if relation is not present?

  15. 15

    Laravel get data from many to many relation

  16. 16

    laravel get relation hasMany model with where clause

  17. 17

    Octobercms/Laravel get id based on relation

  18. 18

    Laravel get a model collection ordered by a relation column

  19. 19

    Laravel collection get occurrences of a MtoM relation

  20. 20

    laravel get relation hasMany model with where clause

  21. 21

    laravel how to get all nested relation to a collection

  22. 22

    Laravel hasMany relation count number of likes and comments on post

  23. 23

    Get full json struct in a belongsTo relation

  24. 24

    Postgres : get min and max rows count in many to many relation table

  25. 25

    how to get data with relation in Laravel trying to get Bill Products to view

  26. 26

    Laravel: Get pivot data for specific many to many relation

  27. 27

    How to get list depending on condition over the relation in Laravel

  28. 28

    Laravel Trying to get property of non-object One to One relation

  29. 29

    Laravel : Error in creating a relation to get first child of each parent entity

HotTag

Archive