ProductStats.php
TLDR
This file defines the ProductStats
class, which extends the StatsOverviewWidget
class. It also uses the InteractsWithPageTable
trait. The ProductStats
class provides a method to specify the table page and another method to define the statistics to be displayed. The statistics include the total number of products, the product inventory, and the average price.
Classes
ProductStats
The ProductStats
class extends the StatsOverviewWidget
class and uses the InteractsWithPageTable
trait. It provides the following methods:
-
getTablePage(): string
- Returns the class name of the table page to be used. -
getStats(): array
- Returns an array ofStat
instances. The statistics include the total number of products, the product inventory, and the average price.
<?php
namespace App\Filament\Resources\Shop\ProductResource\Widgets;
use App\Filament\Resources\Shop\ProductResource\Pages\ListProducts;
use Filament\Widgets\Concerns\InteractsWithPageTable;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
class ProductStats extends BaseWidget
{
use InteractsWithPageTable;
protected static ?string $pollingInterval = null;
protected function getTablePage(): string
{
return ListProducts::class;
}
protected function getStats(): array
{
return [
Stat::make('Total Products', $this->getPageTableQuery()->count()),
Stat::make('Product Inventory', $this->getPageTableQuery()->sum('qty')),
Stat::make('Average price', number_format($this->getPageTableQuery()->avg('price'), 2)),
];
}
}