Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial Premium Tutorial

Filament: One Filter for Multiple Widgets with Livewire Events

August 29, 2023
6 min read

In Filament, adding a filter to dashboard widgets is pretty straightforward. But what if you want to have a filter that would update MULTIPLE widgets at once, similar to Google Analytics? We need to use events and Livewire properties for this. Let's take a look.

filtered two charts


Widgets

First, let's create the widgets. We will need three widgets:

  • Form with date filters
  • Chart with orders
  • Chart with Users
php artisan make:filament-widget Filters
php artisan make:filament-widget OrdersChart --chart
php artisan make:filament-widget UsersChart --chart

For the charts, I will be using the bar type.

Now, let's show date inputs in the Filters widget with Filament Forms. Also, the Filters widget needs to be full width and have the $sort = 1 to be on top.

app/Filament/Widgets/Filters.php:

use Filament\Forms\Form;
use Filament\Forms\Components\Grid;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Concerns\InteractsWithForms;
 
class Filters extends Widget implements HasForms
{
use InteractsWithForms;
 
protected static string $view = 'filament.widgets.filters';
 
protected int | string | array $columnSpan = 'full';
 
protected static ?int $sort = 1;
 
public ?array $data = [];
 
public function form(Form $form): Form
{
return $form
->statePath('data')
->schema([
Grid::make()
->schema([
DatePicker::make('from'),
DatePicker::make('to'),
]),
]);
}
}

In the dashboard, we now have a widget with two date inputs.

filters widget

For the other two widgets, we will show a chart of two models, User and Order. These charts will show how many new records are created daily.

For passing data to the chart, we will use the package recommended by Filament flowframe/laravel-trend.

composer require flowframe/laravel-trend

And for the charts.

app/Filament/Widgets/OrdersChart.php:

use App\Models\Order;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;
use Filament\Widgets\ChartWidget;
 
class OrdersChart extends ChartWidget
{
protected static ?string $heading = 'Orders Chart';
 
protected static ?int $sort = 2;
 
protected function getData(): array
{
$data = Trend::model(Order::class)
->between(
start: now()->subWeek(),
end: now(),
)
->perDay()
->count();
 
return [
'datasets' => [
[
'label' => 'Orders',
'data' => $data->map(fn (TrendValue $value) => $value->aggregate),
],
],
'labels' => $data->map(fn (TrendValue $value) => $value->date),
];
}
 
protected function getType(): string
{
return 'bar';
}
}

app/Filament/Widgets/UsersChart.php:

use App\Models\User;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;
use Filament\Widgets\ChartWidget;
 
class UsersChart extends ChartWidget
{
protected static ?string $heading = 'Users Chart';
 
protected static ?int $sort = 3;
 
protected function getData(): array
{
$data = Trend::model(User::class)
->between(
start: now()->subWeek(),
end: now(),
)
->perDay()
->count();
 
return [
'datasets' => [
[
'label' => 'Users',
'data' => $data->map(fn (TrendValue $value) => $value->aggregate),
],
],
'labels' => $data->map(fn (TrendValue $value) => $value->date),
];
}
 
protected function getType(): string
{
return 'bar';
}
}

We also have two charts in the dashboard showing data from the week to now.

all widgets


Updating Charts

So now, let's update the charts when a filter is selected. To achieve this, we will need to...

Premium Members Only

This advanced tutorial is available exclusively to Laravel Daily Premium members.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel

Comments & Discussion

N
novobyte ✓ Link copied!

thank you for this great tutorial!

MS
Marco Sottana ✓ Link copied!

can you share the repo ?

PK
Povilas Korop ✓ Link copied!

Sorry we didn't save it as a separate repo, as most of the code is in the tutorial itself, anyway.

A
AW ✓ Link copied!

protected static string $view = 'filament.widgets.filters'

It shows empty widget.

N
Nerijus ✓ Link copied!

Such a stupid mistake. You of course need to call table in the Blade {{ $this->form() }}. Will update lesson later.

L
lloricode ✓ Link copied!

I think this is what you mean

{{ $this->form }}
N
Nerijus ✓ Link copied!

You are right

O
OXILYUM ✓ Link copied!

I'm trying to set a default date to the date component with ->default(now) but the value is not set in the component, any idea? Thx for help

N
Nerijus ✓ Link copied!

Now isnt a method, you need to use as a method now()

O
OXILYUM ✓ Link copied!

Yes I used default(now()), but nothing is on the field

MA
Martin Arvisais ✓ Link copied!

Nice tutorial :) Thx! One question: I get a 500 error, Filament\Widgets\ChartWidget::$dataChecksum

Error: Typed property Filament\Widgets\ChartWidget::$dataChecksum must not be accessed before initialization

I didn't found anything about this kind of error... any thought?

M
Modestas ✓ Link copied!

This error happens when you use the variable before it has any value. If you could, please add additional code here.

But few things to look out:

  1. Custom variable has to have a default value set (null is also a value!)
  2. You can't use a variable that has no value as it will not be initialized
PB
Pedro Barradas ✓ Link copied!

More another greate tutorial, but i've a error: Cannot assign Carbon\Carbon to property Flowframe\Trend\Trend::$start of type Illuminate\Support\Carbon

https://flareapp.io/share/Lm8Q0opP

Can you help me?

N
Nerijus ✓ Link copied!

Wrong namespace. Error tells you that

VC
Victor Chaos ✓ Link copied!

This is interesting. Is there a straight forward way to implement a resource form in a widget? Let's say we have a Customer and Order models, where orders belong to customer. We can go to standard CRUD for any of the resources, but could we implement a mini version of that in a widget? So we can quickly select a Customer and fill out minimum required fields for Order. I think it would be useful when on mobile phone for example, and when you gat back to a PC fill the rest of the forms with standard full model CRUD.

N
Nerijus ✓ Link copied!

What do you mean in a widget?

VC
Victor Chaos ✓ Link copied!

I mean like dashboard widget with a form to add a record for a specific resource. Basically a resouce form in a custom widget to quickly add a record without clicking Orders->Create->Fill Out Form->save

N
Nerijus ✓ Link copied!

In custom widget you can have whatever you want.

VC
Victor Chaos ✓ Link copied!

I figured, yes. I just mean an example for us noobs. I can't figure out how to specify a Resource model for the form in a widget.

Might even be an interesting topic to take a stab at.

N
Nerijus ✓ Link copied!

Don't think you can. Treat widget as a livewire component.

JM
Jon Mason ✓ Link copied!

Any idea how to do this same type thing using the ApexCharts plugin for filament? It's mostly working based on your tutorial, but I can't get the chart to re-render even after using ->reactive() and -afterStateUpdated()

N
Nerijus ✓ Link copied!

No idea. Haven't tried it with that plugin

B
bogusbd ✓ Link copied!

Good tutorial, very helpful. Thanks!

But for some reason I couldn't get this to work with Filament\Widgets\StatsOverviewWidget .. Maybe I'm using a newer version Filament.

Anyway, the official documentation also contains examples of how to implement similar functional https://filamentphp.com/docs/3.x/panels/dashboard

And it’s funny that I first found the solution on laraveldaily.com and not on the official website))

N
Nerijus ✓ Link copied!

Because this festure was released just yesterday

HM
Hossam Mohamed ✓ Link copied!

How to handle the performance and speed issues related to use Widgets !!

the performance is so slow and bad

N
Nerijus ✓ Link copied!

Hard to answer without seeing the issue and what is slow. Don't if possible but adding loading indicators should feel better atleast

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.