On the resource model index page, you can display informational blocks with statistics - metrics.
To do this, return an array of Metric in the metrics() method.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:start]
namespace App\MoonShine\Resources;
use App\Models\Post;
use App\Models\Comment;
use MoonShine\UI\Components\Metrics\Wrapped\Metric;
use MoonShine\UI\Components\Metrics\Wrapped\ValueMetric;
use MoonShine\Laravel\Resources\ModelResource; // [tl! collapse:end]
class PostResource extends ModelResource
{
// ...
/**
* @return list
*/
protected function metrics(): array
{
return [
ValueMetric::make('Articles')
->value(fn() => Post::count())
->columnSpan(6),
ValueMetric::make('Comments')
->value(fn() => Comment::count())
->columnSpan(6),
];
}
}
use Closure;
use MoonShine\Laravel\Components\Fragment;
protected function fragmentMetrics(): ?Closure
{
return static fn(array $components): Fragment => Fragment::make($components)->name('metrics');
}