Laravel: Order query by count id field on relation

Miguelopezv

I have a resources table, and a resources_votes table in which I have a record for everytime a user likes [vote_id 1] or dislikes [vote_id 2] a resource. now I need to retrieve all the resource information ordered from most liked to the less one, and since resource has many resources_votes when I use ->with('votes') it returns an array of objects with each one of the resources_votes related to the resource ID.

Is there a way I can count how many positive votes a resource has [vote_id =2], add a field with this count and order the query from most voted to less voted?

PD: this is an example of a resource object with the resources_votesrelationship, there you can see the votes array and the vote ID which I need to count and order according to:

    {
            "id": 2,
            "name": "aspernatur",
            "image": "http://lorempixel.com/480/480/?31738",
            "author": "Max Thiel",
            "created_by": 6,
            "reviewed_by": "Mr. Emiliano Frami",
            "lang_id": 2,
            "resource_type_id": 1,
            "status": "Borrado",
            "resource_type": "Imagen",
            "platforms": [],
            "classifications": [],
            "votes": [
              {
                "id": 2,
                "user_id": 2,
                "resource_id": 2,
                "vote_id": 1
              },
              {
                "id": 29,
                "user_id": 1,
                "resource_id": 2,
                "vote_id": 2
              },
              {
                "id": 24,
                "user_id": 12,
                "resource_id": 2,
                "vote_id": 1
              },
            ]
          },
EddyTheDove

You can get it with eager loading like this

$resources = Resource::withCount(['votes' => function ($query) {
    $query->where('vote_id', '=', '2'); //count positive votes only
}])->get();

This will return a column named votes_count. You need to call that column to display the count.

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 4: Order query relation randomly and limited

From Dev

count query using eloquent relation laravel

From Dev

Yii2: how to order activeRecord relation query by related field?

From Dev

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

From Dev

Count Relation in Eloquent Query

From Dev

Laravel query that append child count field

From Dev

Laravel Eloquent order Relation

From Dev

Laravel order by relation with join

From Dev

Error in fetching id of a model and updating a field using the Laravel HasManyThrough relation

From Dev

Laravel relation total count

From Dev

Order by eager loaded relation count

From Dev

Laravel eloquent relation query

From Dev

Order query by special relation and paginate

From Dev

Relation field query not working as expected

From Dev

Get relation count Laravel JSON

From Dev

Use select count in relation Laravel

From Dev

Laravel Query Builder Subtract COUNT by Subquery in query grouped by ID

From Dev

OrderBy count appearing in the wrong order in Eloquent Query Builder (Laravel 4)

From Dev

Using Laravel's Schema builder is there a way to do a ORDER BY FIELD query?

From Dev

Yii2 MySQL Order By Relation Count

From Dev

How to query relation in laravel scope

From Dev

Laravel 5.3 query (group by 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

Mongoid - Use relation field in where query

From Dev

Mongoid - Use relation field in where query

From Dev

Need to find count of shipment id which corresponds to certain set of order id in sql table but query is buggy

From Dev

Count number of ID in relation and connect to name in PostgreSQL

From Dev

Laravel DB query with count

Related Related

  1. 1

    Laravel 4: Order query relation randomly and limited

  2. 2

    count query using eloquent relation laravel

  3. 3

    Yii2: how to order activeRecord relation query by related field?

  4. 4

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

  5. 5

    Count Relation in Eloquent Query

  6. 6

    Laravel query that append child count field

  7. 7

    Laravel Eloquent order Relation

  8. 8

    Laravel order by relation with join

  9. 9

    Error in fetching id of a model and updating a field using the Laravel HasManyThrough relation

  10. 10

    Laravel relation total count

  11. 11

    Order by eager loaded relation count

  12. 12

    Laravel eloquent relation query

  13. 13

    Order query by special relation and paginate

  14. 14

    Relation field query not working as expected

  15. 15

    Get relation count Laravel JSON

  16. 16

    Use select count in relation Laravel

  17. 17

    Laravel Query Builder Subtract COUNT by Subquery in query grouped by ID

  18. 18

    OrderBy count appearing in the wrong order in Eloquent Query Builder (Laravel 4)

  19. 19

    Using Laravel's Schema builder is there a way to do a ORDER BY FIELD query?

  20. 20

    Yii2 MySQL Order By Relation Count

  21. 21

    How to query relation in laravel scope

  22. 22

    Laravel 5.3 query (group by relation)

  23. 23

    How to get count of relation field in parse

  24. 24

    count query - how to get relation instead of hash

  25. 25

    Mongoid - Use relation field in where query

  26. 26

    Mongoid - Use relation field in where query

  27. 27

    Need to find count of shipment id which corresponds to certain set of order id in sql table but query is buggy

  28. 28

    Count number of ID in relation and connect to name in PostgreSQL

  29. 29

    Laravel DB query with count

HotTag

Archive