Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mravdel's avatar
Level 11

How to make Nova metric on several models

Hi! I have 3 different models, each of them have "balance" property. I need to show metric that sum all of balances of all 3 models. How can I do that? Can I use metrics or I need to create my own custom card?

0 likes
1 reply
tisuchi's avatar

@mravdel

What if you try this?


use Laravel\Nova\Metrics\Metric;
use Illuminate\Http\Request;

class TotalBalanceMetric extends Metric
{
    public function calculate(Request $request)
    {
        $balance1 = Model1::sum('balance');
        $balance2 = Model2::sum('balance');
        $balance3 = Model3::sum('balance');

        $total = $balance1 + $balance2 + $balance3;

        return $this->result($total)
                    ->format('0,0');
    }

    public function uriKey()
    {
        return 'total-balance';
    }
}

To display the metric in your Nova dashboard, you can use the TotalBalanceMetric class in your dashboard resource:


use App\Nova\Metrics\TotalBalanceMetric;

class Dashboard extends Resource
{
    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [
            (new TotalBalanceMetric)->width('1/3'),
        ];
    }
}

Please or to participate in this conversation.