ModelResource

Metrics

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.

 namespaces
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;
 
class PostResource extends ModelResource
{
// ...
 
/**
* @return list<Metric>
*/
protected function metrics(): array
{
return [
ValueMetric::make('Articles')
->value(fn() => Post::count())
->columnSpan(6),
ValueMetric::make('Comments')
->value(fn() => Comment::count())
->columnSpan(6),
];
}
}
 namespaces
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;
 
class PostResource extends ModelResource
{
// ...
 
/**
* @return list<Metric>
*/
protected function metrics(): array
{
return [
ValueMetric::make('Articles')
->value(fn() => Post::count())
->columnSpan(6),
ValueMetric::make('Comments')
->value(fn() => Comment::count())
->columnSpan(6),
];
}
}

metrics metrics_dark

For more detailed information, refer to the sections Metrics.

If you need to wrap the metrics in a Fragment:

 namespaces
use Closure;
use MoonShine\Laravel\Components\Fragment;
 
protected function fragmentMetrics(): ?Closure
{
return static fn(array $components): Fragment => Fragment::make($components)->name('metrics');
}
 namespaces
use Closure;
use MoonShine\Laravel\Components\Fragment;
 
protected function fragmentMetrics(): ?Closure
{
return static fn(array $components): Fragment => Fragment::make($components)->name('metrics');
}