Giter Club home page Giter Club logo

nova-linkable-metrics's Introduction

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\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.

nova-linkable-metrics's People

Contributors

anderly avatar dependabot[bot] avatar lorenzosapora avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.