saintsystems/nova-linkable-metrics

Linkable metrics for Laravel Nova.

0.1.1 2021-04-01 17:14 UTC

README

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\LinkableValue;

use LinkableValue;

Within your custom Nova trend metric card:

// in your Nova value metric card class:
import SaintSystems\Nova\LinkableMetrics\LinkableTrend;

use LinkableTrend;

Within your custom Nova trend partition card:

// in your Nova value metric card class:
import SaintSystems\Nova\LinkableMetrics\LinkablePartition;

use LinkablePartition;

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:

Index Route

    // 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 using a Lens Route

    // 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('lens', ['resourceName' => 'jobs', 'lens' => 'all-jobs']),
        ];
    }

OR using a Filter Route

    // NovaServiceProvider.php

    /**
     * Get the cards that should be displayed on the Nova dashboard.
     *
     * @return array
     */
    protected function cards()
    {
        $filters = base64_encode(json_encode([
            [
                'class' => JobStatus::class,
                'value' => 'active',
            ],
        ]));

        return [
            (new JobsInProgress)->width('1/3')->route('lens', ['resourceName' => 'jobs'], ['jobs_filter' => $filters]),
        ];
    }
  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.