mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 01:08:56 +00:00
28 lines
738 B
PHP
28 lines
738 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class RoleStat extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $guarded = [];
|
|
|
|
public static function insertUsersByRole(): void
|
|
{
|
|
$roles = Role::query()->select(['name'])->withCount('users')->groupBy('name')->having('users_count', '>', 0)->orderByDesc('users_count')->get();
|
|
foreach ($roles as $role) {
|
|
self::updateOrCreate(['role' => $role->name, 'users' => $role->users_count]);
|
|
}
|
|
}
|
|
|
|
public static function getUsersByRole(): array
|
|
{
|
|
return self::query()->select(['role', 'users'])->get()->toArray();
|
|
}
|
|
}
|