Silvanite / novatoolpermissions

Laravel Nova Permissions Tool (User, Roles and Permissions / Access Control (ACL))
MIT License
101 stars 33 forks source link

Role in metirc #48

Closed mjlandicho closed 4 years ago

mjlandicho commented 5 years ago

how can i count data from Role model into my metric? I mean, I want to show in metric if how many admins, users. in my database users

mjlandicho commented 5 years ago

Capture

m2de commented 5 years ago

You should be able to access the Role model like any other to get the data you need. It has a users() relationship which you can use to count the number of users on each model. E.g. Role::first()->users()->count() Please let me know if you need any more info. Cheers.

abordage commented 5 years ago
public function calculate(Request $request)
{
    $result = Role::withCount([
        'users' => static function (Builder $query) {
            $query->whereNull('banned_at');
        },
    ])
        ->orderBy('users_count', 'desc')
        ->get()
        ->mapWithKeys(static function ($item) {
            return [$item['name'] => $item['users_count']];
        })
        ->put('Banned', User::whereNotNull('banned_at')->count())
        ->toArray();

    return $this->result($result);
}