Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Added GlobalFilterableTrait for auto handling filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
MuzafferDede committed May 5, 2020
1 parent 9da724d commit 4908547
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 44 deletions.
91 changes: 50 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Nova Global Filter

This package allows you to broadcast any of your existing Laravel Nova filters to Metrics or custom cards.
This package allows you to broadcast any of your existing Laravel Nova filters to metrics or custom cards.

![screenshot](https://user-images.githubusercontent.com/5906125/80741990-3075f180-8b4d-11ea-8b39-c7bd03d9d7b5.png)
![screenshot](resources/gifs/nova-global-filter.gif)

## Installation

Expand All @@ -14,65 +14,74 @@ composer require nemrutco/nova-global-filter

## Usage

In this example, we are registering few `Metric Cards` and the `Global Filter` as:
In this example, we are registering few `Metric Cards` and the `Global Filter` with a `Date` filter as:

```php
public function cards(Request $request)
{
return [
new NewUsers // Value Metric

new UsersPerDay, // Trend Metric

new UsersPerPlan, // Partition Metric

new \Nemrutco\NovaGlobalFilter([
new UserType, // Select Filter

new BirthdayFilter, // Date Filter
...
use Nemrutco\NovaGlobalFilter;
use App\Nova\Filters\Date;

new UserGender // Booloean Filter
]),
];
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 on `Metric Card` or any other cards `filters` are available in `$request`:
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:

```php
use Nemrutco\NovaGlobalFilter\GlobalFilterable;
use App\Nova\Filters\Date;
...

class UsersPerDay extends Trend
{
public function calculate(NovaRequest $request)
{
// Get the model query before applying filters
$model = User::query();

// Check if there is any filters in the request
if($request->has('filters')){
// Apply each filter to the $model
foreach(json_decode($request->filters,true) as $filter => $value) {
$model = (new $filter)->apply($request,$model,$value);
}
}

// Do your thing with the filtered $model
return $this->countByDays($request, $model);
}
...
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`:

```js
...
created() {
Nova.$on("global-filter-changed", filter => {
// Do your thing with the changed filter
console.log(filter);
});
Nova.$on("global-filter-changed", filter => {
// Do your thing with the changed filter
console.log(filter);
});
},
...
```
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{
"name": "nemrutco/nova-global-filter",
"description": "A Laravel Nova card.",
"description": "This package allows you to broadcast any of your existing Laravel Nova filters to metrics or custom cards.",
"keywords": [
"laravel",
"nova"
"nova",
"filters",
"dashboard",
"metrics",
"card"
],
"license": "MIT",
"require": {
"php": ">=7.1.0"
"php": ">=7.1.0",
"laravel/nova": ">=2.0"
},
"autoload": {
"psr-4": {
Expand Down
Binary file added resources/gifs/nova-global-filter.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/GlobalFilterable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Nemrutco\NovaGlobalFilter;

use Illuminate\Database\Eloquent\Builder;

trait GlobalFilterable
{
public function globalFiltered($model, $filters = [])
{
$request = request();
if ($request->has('filters')) {
$request->range = optional($request)->range ?? 3600;

$model = $model instanceof Builder ? $model : (new $model)->newQuery();

foreach (json_decode($request->filters, true) as $filter => $value) {
if(in_array($filter, $filters)){
$model = (new $filter)->apply($request, $model, $value);
}
}
}
return $model;
}
}

0 comments on commit 4908547

Please sign in to comment.