PHP code example of creagia / nova-percentage-card
1. Go to this page and download the library: Download creagia/nova-percentage-card library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
creagia / nova-percentage-card example snippets
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(),
];
}