DEV Community

Cover image for πŸ“Š Personalized Dashboards: Dynamic Widget Registration in Filament
Vitalik
Vitalik

Posted on

3 3 1 2 1

πŸ“Š Personalized Dashboards: Dynamic Widget Registration in Filament

πŸ‘‹ Hi, in this post, I’ll show you how to dynamically register widgets in Filament based on user properties. This approach allows you to display personalized widgets, such as task overviews πŸ“‹, for each user. We’ll walk through a simplified example using a Task model with a name property and a many-to-one relationship with the User model.

πŸš€ So let’s start!

1) Create custom livewire widget, this is important that they are not auto discover from Filament/Widgets folder by default

Create Widget

2) Our widget has a $task property and display its name

<?php

namespace App\Livewire;

use App\Models\Task;
use Filament\Widgets\Widget;

class TaskOverview extends Widget
{
    protected static string $view = 'livewire.task-overview';

    public Task $task;

    public function mount(Task $task): void
    {
        $this->task = $task;
    }
}
Enter fullscreen mode Exit fullscreen mode
<x-filament-widgets::widget>
    <x-filament::section>
        Task name: {{ $task->name }}
    </x-filament::section>
</x-filament-widgets::widget>
Enter fullscreen mode Exit fullscreen mode

3) Create middleware for register widget

php artisan make:middleware AddWidgetForUserMiddleware
Enter fullscreen mode Exit fullscreen mode
public function handle(Request $request, Closure $next): Response
{
    if (is_null($request->user())) {
        return $next($request);
    }

    if (! filament()->getCurrentPanel()) {
        return $next($request);
    }

    $widgets = [];

    foreach (auth()->user()->tasks as $task) {
        $widgets[] = TaskOverview::make(['task' => $task]);
    }

    filament()->getCurrentPanel()->widgets($widgets);

    return $next($request);
}
Enter fullscreen mode Exit fullscreen mode

4) Last step its register our middleware in AdminPanelProvider

->middleware([
    EncryptCookies::class,
    AddQueuedCookiesToResponse::class,
    StartSession::class,
    AuthenticateSession::class,
    ShareErrorsFromSession::class,
    VerifyCsrfToken::class,
    SubstituteBindings::class,
    DisableBladeIconComponents::class,
    DispatchServingFilamentEvent::class,

    AddWidgetForUserMiddleware::class,
])
Enter fullscreen mode Exit fullscreen mode

That's what the user sees if he has tasks.

Dynamic Widgets

πŸ’‘ This approach allows for highly personalized dashboards in Filament, making user experiences more dynamic. I hope you found this tutorial helpful! πŸ™Œ If you have any questions or ideas for improvements, feel free to share them in the comments below πŸ’¬

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit