Skip to content

DigitalCloud/nova-linkable-metrics

 
 

Repository files navigation

Nova Linkable Metrics

Latest Version on Packagist Total Downloads

Add custom router-links to your Laravel Nova metrics.

screenshot

Installation

You can install the package in to a Laravel app that uses Nova via composer:

composer require saintsystems/nova-linkable-metrics

Usage

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;

Defining Metric Links

You can define metric links using the route method from the Linkable trait in one of two ways:

  1. 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']),`
        ];
    }
  1. 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);
    }

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Nova Linkable Metrics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Vue 80.6%
  • PHP 16.1%
  • JavaScript 3.2%
  • CSS 0.1%