Laravel 5 Eloquent count many to many relationship

user1294914

Let's say I have table user and roles with many to many relationships using pivot table role_user table.

I'm using belongstomany relationships on my model

How do I make an eloquent query to count how many user has role admin and staff

user1294914

solved.

add this to Role.php model

public function userCount() {
    return $this->belongsToMany(Role::class)
        ->selectRaw('count(role_user.user_id) as total_user')
        ->groupBy('role_id');
}

and this

public function getUserCountAttribute()
    {
        if ( ! array_key_exists('userCount', $this->relations)) $this->load('customerCount');

        $related = $this->getRelation('userCount')->first();

        return ($related) ? $related->total_user : 0;
    }

after that, to make an eloquent query...

$roleUsers = Role::with('userCount')->orderBy('id', 'asc')->get();

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 5 Eloquent Relationship - Has Many Though

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel many to many relationship using Eloquent

From Dev

Laravel whereHas count on Many-to-Many relationship

From Dev

Laravel 5 Eloquent ORM - Many to Many through Many to Many

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

laravel update, remove one to many relationship eloquent

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel eloquent update one-many relationship

From Dev

One to Many to One relationship in laravel eloquent

From Dev

eloquent: query many to many relationship

From Dev

Laravel save many-to-many relationship in Eloquent mutators

From Dev

Laravel Eloquent: How to select not attached records in many to many relationship?

From Dev

Need help to structure Eloquent Many to Many relationship (laravel 5.1)

From Dev

Laravel 5.1 Eloquent distant relationship with many to many models

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Many to many Laravel Eloquent relationship with multiple intermediate tables

From Dev

Laravel - Many to Many relationship

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Laravel count number of rows in has many relationship

From Dev

Laravel count number of rows in has many relationship

From Dev

Laravel 5 Eloquent Many to Many 2ndary table

From Dev

Laravel 5 Deleting a one-to-many relationship

From Dev

Laravel Eloquent Many to Many Query

From Dev

Laravel many to many selecting with Eloquent

Related Related

  1. 1

    Laravel 5 Eloquent Relationship - Has Many Though

  2. 2

    Laravel 5 one to many eloquent relationship

  3. 3

    Laravel 5 Eloquent Relationship - Has Many Though

  4. 4

    Laravel many to many relationship using Eloquent

  5. 5

    Laravel whereHas count on Many-to-Many relationship

  6. 6

    Laravel 5 Eloquent ORM - Many to Many through Many to Many

  7. 7

    Laravel 5 | Many to Many Relationship not working

  8. 8

    laravel update, remove one to many relationship eloquent

  9. 9

    Laravel eloquent update one-many relationship

  10. 10

    Laravel eloquent update one-many relationship

  11. 11

    One to Many to One relationship in laravel eloquent

  12. 12

    eloquent: query many to many relationship

  13. 13

    Laravel save many-to-many relationship in Eloquent mutators

  14. 14

    Laravel Eloquent: How to select not attached records in many to many relationship?

  15. 15

    Need help to structure Eloquent Many to Many relationship (laravel 5.1)

  16. 16

    Laravel 5.1 Eloquent distant relationship with many to many models

  17. 17

    Laravel Eloquent Many to Many relationship using a pivot table

  18. 18

    Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

  19. 19

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  20. 20

    Many to many Laravel Eloquent relationship with multiple intermediate tables

  21. 21

    Laravel - Many to Many relationship

  22. 22

    Many to many relationship in Laravel

  23. 23

    Many to Many relationship with Laravel

  24. 24

    Laravel count number of rows in has many relationship

  25. 25

    Laravel count number of rows in has many relationship

  26. 26

    Laravel 5 Eloquent Many to Many 2ndary table

  27. 27

    Laravel 5 Deleting a one-to-many relationship

  28. 28

    Laravel Eloquent Many to Many Query

  29. 29

    Laravel many to many selecting with Eloquent

HotTag

Archive