How to Show Different Stats Widgets to Different Roles in FilamentPHP

When building dashboards in FilamentPHP, you may want to display different statistics to different user roles.
For example:

  • Admins → can see total users
  • Admins + Editors → can see total posts

Filament makes this very easy by using simple role or permission checks inside your widget.

1. Create your StatsOverviewWidget

php artisan make:filament-widget StatsOverview --stats-overview

The above command will generate the StatsOverview file.

class StatsOverview extends StatsOverviewWidget
{
    protected function getStats(): array
    {
        $stats = Cache::remember('dashboard_stats',now()->addMinute(5),function(){
                $posts = Post::published()
                            ->selectRaw("
                                SUM(CASE WHEN type = 'post' THEN 1 ELSE 0 END) as total_posts,
                                SUM(CASE WHEN type = 'page' THEN 1 ELSE 0 END) as total_pages
                            ")
                            ->first();

                return [
                    'totalPosts' => $posts->total_posts,
                    'totalPages' => $posts->total_pages,
                    'totalUsers' => User::count()
                ];
        });
        return [
            Stat::make('Total Post',$stats['totalPosts']),
            Stat::make('Total Pages',$stats['totalPages']),
            Stat::make('Total Users', $stats['totalUsers'])
        ];
    }
}

Permission

Hide the entire widget

Lets say, we want to show the overall widget on the basis of single permission “can_see_widgets”, then we can override the static canView() method.

📚 Learn Accounting the Easy Way!

Step-by-step lessons, examples & exercises for beginners

✨ Beginner-Friendly 📖 Practice Questions
Download on Google Play
📚
Learn
Accounting
public static function canView(): bool
{
    return auth()->user()->can('can_see_widgets');
}

Conditional Permission

Now, lets say only admin users can see total users and both admin and editors can see posts, pages and users count.

protected function getStats(): array
{
    $stats = Cache::remember('dashboard_stats', now()->addMinutes(5), function () {
        $posts = Post::published()
            ->selectRaw("
                SUM(CASE WHEN type = 'post' THEN 1 ELSE 0 END) as total_posts,
                SUM(CASE WHEN type = 'page' THEN 1 ELSE 0 END) as total_pages
            ")
            ->first();

        return [
            'totalPosts' => $posts->total_posts,
            'totalPages' => $posts->total_pages,
            'totalUsers' => User::count(),
        ];
    });

    $cards = [];

    // For roles that can see posts & pages
   if (auth()->user()->hasAnyRole(['Admin', 'Editor'])) {
     $cards[] = Stat::make('Total Posts', $stats['totalPosts']);
     $cards[] = Stat::make('Total Pages', $stats['totalPages']);
   }

    // Only Admin can see users
   if (auth()->user()->hasRole('Admin')) {
     $cards[] = Stat::make('Total Users', $stats['totalUsers']);
   }


    return $cards;
}

Conclusion

With just a few role checks, you can create a fully dynamic dashboard where each user sees only the stats relevant to their role

Leave a Reply

Your email address will not be published. Required fields are marked *