creagia/nova-percentage-card

适用于 Nova 应用程序的百分比卡片

2.1.0 2021-02-01 16:12 UTC

This package is auto-updated.

Last update: 2024-09-13 17:54:18 UTC


README

一个用于显示百分比的 Laravel Nova 卡片

alt text

支持我们

Laradir banner

要求

  • php: >=7.1
  • laravel/nova: ^2.0|^3.0

安装

composer require creagia/nova-percentage-card

本地化

将包的语言文件发布到应用程序的 resources/lang/vendor 目录

php artisan vendor:publish --provider="Creagia\NovaPercentageCard\CardServiceProvider"

用法

  • 创建一个继承自 \Creagia\NovaPercentageCard\NovaPercentageCard 的类
  • cards() 方法中添加一个新的对象

基本示例

class SpentBudget extends \Creagia\NovaPercentageCard\NovaPercentageCard
{
    /**
     * Get the total of filtered records
     *
     * @return float
     */
    function getCount(): float
    {
        return Order::sum('total');
    }

    /**
     * Get the number of the total records
     *
     * @return float
     */
    function getTotal(): float
    {
        return config('app.total_budget');
    }
}
class NovaServiceProvider extends NovaApplicationServiceProvider

...

/**
 * Get the cards that should be displayed on the default Nova dashboard.
 *
 * @return array
 */
protected function cards()
{
    return [
        new SpentBudget,
    ];
}

完整示例

class SpentBudget extends \Creagia\NovaPercentageCard\NovaPercentageCard
{
    /**
     * The displayable name of the metric.
     *
     * @var string
     */
    protected $name = 'Spent budget';
    
    /**
     * The label for the total entries.
     *
     * @var string|null
     */
    protected $label = '';

    /**
     * The number of decimal points
     *
     * @var int
     */
    protected $percentagePrecision = 2;

    /**
     * The width of the card (1/3, 1/2, or full).
     *
     * @var string
     */
    public $width = '1/3';

    /**
     * Get the total of filtered records
     *
     * @return float
     */
    function getCount(): float
    {
        return Order::sum('total');
    }

    /**
     * Get the number of the total records
     *
     * @return float
     */
    function getTotal(): float
    {
        return config('app.total_budget');
    }
    
    /**
     * Determine for how many time the metric should be cached.
     *
     * @return  \DateTimeInterface|\DateInterval|float|int
     */
    public function cacheFor(): int
    {
        return now()->addMinutes(5);
    }

    /**
     * Determine the cache key
     *
     * @return  string
     */
    public function cacheKey(): string
    {
        return 'spent-budget-percentage-card';
    }
}

资源详情示例

class OrderTotalPaid extends \Creagia\NovaPercentageCard\NovaPercentageCard
{
    function getCount(): float
    {
        return \App\Models\Order::find($this->getResourceId())->total_paid;
    }

    function getTotal(): float
    {
        return \App\Models\Order::find($this->getResourceId())->total;
    }
}

...

public function cards(Request $request)
{
    $percentageCard = new OrderTotalPaid();
    $percentageCard->setResourceId($request->resourceId ?? null);
    return [
        $percentageCard->onlyOnDetail(),
    ];
}

许可证

本项目采用 MIT 许可证授权 - 有关详细信息,请参阅 MIT 许可证文件