by Nemrut Creative Studio on 01 May 2020
This package allows you to broadcast any of your existing Laravel Nova filters to metrics or custom cards.
You can install the package in to a Laravel
app that uses Nova via composer:
composer require nemrutco/nova-global-filter
In this example, we are registering few Metric Cards
and the Global Filter
with a Date
filter as:
...
use Nemrutco\NovaGlobalFilter;
use App\Nova\Filters\Date;
class Store extends Resource
{
...
public function cards(Request $request)
{
return [
new TotalSales // Value Metric
new Orders, // Trend Metric
new MostSoldProduct, // Partition Metric
// NovaGlobalFilter
new NovaGlobalFilter([
new Date, // Date Filter
]),
];
}
...
}
And now your metric cards
or any other cards
optimized to listen GlobalFilter
can be filtered by using GlobalFilterable
trait and calling $this->globalFiltered($model,$filters)
method.
globalFiltered($model, $filters = [])
method expect $model
and $filters
parameters:
use Nemrutco\NovaGlobalFilter\GlobalFilterable;
use App\Nova\Filters\Date;
...
class UsersPerDay extends Trend
{
use GlobalFilterable;
public function calculate(NovaRequest $request)
{
// Filter your model with existing filters
$model = $this->globalFiltered(Store::class,[
Date::class // DateFilter
]);
// Do your thing with the filtered $model
return $this->countByDays($request, $model);
}
...
}
And that's it. Your model will be filtered based on passed filter value.
if you want to listen to Global Filter
on any of Custom Cards
:
...
created() {
Nova.$on("global-filter-changed", filter => {
// Do your thing with the changed filter
console.log(filter);
});
},
...
Nova.$on('global-filter-changed', [changed filter and value])
. So any card that listens to this event, will recieve the filter and it's value.Metric Cards
to allow them to listen "global-filter-changed"
event. Make sure there are no any conflicts with other pacages.Global Filter
will not trigger update at the filters in Filter Menu
of your Index view.The MIT License (MIT). Please see License File for more information.
Copyright ©2021 Nemrut Creative Studio