Add custom router-links to your Laravel Nova metrics.
You can install the package in to a Laravel app that uses Nova via composer:
composer require saintsystems/nova-linkable-metrics
To add the link ability to your Laravel Nova metric cards, you need to add the Linkable
traits to your metrics.
For example, within your custom Nova value metric card:
// in your Nova value metric card class:
import SaintSystems\Nova\LinkableMetrics\Linkable;
use Linkable;
You can define metric links using the route
method from the Linkable
trait in one of two ways:
- When the card is registered:
// NovaServiceProvider.php
/**
* Get the cards that should be displayed on the Nova dashboard.
*
* @return array
*/
protected function cards()
{
return [
(new JobsInProgress)->width('1/3')->route('index', ['resourceName' => 'jobs']),`
];
}
- Or, within the card itself (useful for cards only available on detail screens where you might want to filter the url based on the current resource):
// In your linktable Nova metric class
/**
* Calculate the value of the metric.
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
public function calculate(Request $request, UnitOfMeasure $unitOfMeasure)
{
$result = $this->result($unitOfMeasure->items()->count());
$params = ['resourceName' => 'items'];
$query = [
'viaResource' => $request->resource,
'viaResourceId' => $unitOfMeasure->id,
'viaRelationship' => 'items',
'relationshipType' => 'hasMany',
];
return $result->route('index', $params, $query);
}
The MIT License (MIT). Please see License File for more information.