Filament has a great customizable dashboard. But what if you want to show different things there, for different users, like administrators and simple users? With Filament, it's easy.
In this example, we have User
and Task
models.
The goal of different dashboards is this:
- Admins will see cards with numbers like "Total users", "Users Registered Today" and "Tasks Created Today".
- Non-admins will just see "Tasks Due Today" and some greeting text
Final admin view:
Final non-admin view:
In fact, it won't be multiple dashboards, it will be one dashboard with different widget settings for each role.
For this quick example, the Admin user will be set in the Users
table by setting the is_admin
field to true.
The Task
model will have with only two fields: title
and due_date
.
Conditionally Hiding Widgets
First, let's make widgets that only the admin user will be able to see.
We need to create a widget with three cards: total users count, users registered today count, and tasks created today count.
php artisan make:filament-widget AdminWidgets --stats-overview
app/Filament/Widgets/AdminWidgets.php:
use App\Models\User;use App\Models\Task;use Filament\Widgets\StatsOverviewWidget as BaseWidget;use Filament\Widgets\StatsOverviewWidget\Card; class AdminWidgets extends BaseWidget{ protected function getCards(): array { return [ Card::make('Total Users', User::count()), Card::make('Users Registered Today', User::whereDate('created_at', today())->count()), Card::make('Tasks Created Today', Task::whereDate('created_at', today())->count()), ]; }}
After visiting the Dashboard page, you will see three cards.
But if you visit Dashboard with the non-admin user, you would also see those cards. To tell widgets who can see them, we just need to use the canView()
method.
app/Filament/Widgets/AdminWidgets.php:
class AdminWidgets extends BaseWidget{ protected static ?int $sort = 2; protected function getCards(): array { return [ Card::make('Total Users', User::count()), Card::make('Users Registered Today', User::whereDate('created_at', today())->count()), Card::make('Tasks Created Today', Task::whereDate('created_at', today())->count()), ]; } public static function canView(): bool { return auth()->user()->is_admin; } }
Now only users with is_admin
set to true will see this widget.
Let's create another widget that will show users' Tasks due today.
php artisan make:filament-widget TasksDueToday --stats-overview
app/Filament/Widgets/TasksDueToday.php:
class TasksDueToday extends BaseWidget{ protected function getCards(): array { return [ Card::make('Tasks Due Today', auth()->user()->tasks()->whereDate('due_date', today())->count()), ]; }}
By default, everyone can see widgets, so we don't need to do anything extra.
Custom Dashboard
If you would like to customize the dashboard, you need to create a new file at app/Filament/Pages/Dashboard.php
which needs to extend the original dashboard.
app/Filament/Pages/Dashboard.php:
use Filament\Pages\Dashboard as BasePage; class Dashboard extends BasePage{ // ...}
And remove the original Dashboard
class from the configuration file.
config/filament.php:
'pages' => [ // ... 'register' => [],],
Now, because Dashboard is just a Filament Page, you can add everything that page can have. For example, let's add a subheading that will only be shown if the user isn't an admin.
app/Filament/Pages/Dashboard.php:
use Illuminate\Contracts\Support\Htmlable;use Filament\Pages\Dashboard as BasePage; class Dashboard extends BasePage{ protected function getSubheading(): string | Htmlable | null { if (auth()->user()->is_admin) { return null; } return 'Here you will see an overview of your tasks.'; }}
And that's it, here's how to customize Filament dashboard by user roles.
If you want more Filament examples, you can find more real-life projects on our FilamentExamples.com.
I've read this tutorial, and i want to know if is possible to create an stats overview, for example, to each product, of a list of product that have name and stock number, and how can i do it? Thanks!
This tutorial is actually for Filament 2, before they released multiple panels feature in Filament 3.
But your question is not about that. I guess you create a Stats Overview widget and then inside do
foreach ($products as $product) { Stat::make($product->name, $product->some_number); }
Would it be possible to put the info that this is for Filament 2 somewhere in the top of the article please. It is quite confusing. In general I'd suggest to always add this information to and article.
Will check if this can be an option. The biggest problem is manually updating the articles and some of them - might even work in Filament 3. So it's best to check the article posting date and treat it accordingly. Anything before mid july is Filament v2