Fawzan
Fawzan

Reputation: 4849

Displaying the value in a Laravel Nova Value metric

I need to show the sum of the price column of model Foo. Right now I can do it with this.

    public function calculate(Request $request)
    {

        return $this
            ->sum($request, Contribution::class, 'contribution_amount')
            ->dollars();
    }

Which show the following output.

I need to just show $22, $3120 without any formatting. I tried to override the aggregate function but it still doesn't give me the correct output format.

protected function aggregate($request, $model, $function, $column = null, $dateColumn = null)
    {
        $query = $model instanceof Builder ? $model : (new $model)->newQuery();

        $column = $column ?? $query->getModel()->getQualifiedKeyName();

        $previousValue = with(clone $query)->whereBetween(
            $dateColumn ?? $query->getModel()->getCreatedAtColumn(), $this->previousRange($request->range)
        )->{$function}($column);

        return $this->result(
            with(clone $query)->whereBetween(
                $dateColumn ?? $query->getModel()->getCreatedAtColumn(), $this->currentRange($request->range)
            )->{$function}($column)
        )->previous($previousValue);
    }

Can anyone give a pointer here?

Upvotes: 3

Views: 3240

Answers (2)

Fawzan
Fawzan

Reputation: 4849

For future readers..

As of Nova 1.3.1 we can use Numeral.js formatting to format the values in the Trend/Value metric.

return $this
            ->result($val)
            ->dollars()
            ->format('0,0.00')

Above snippet will format the value to be displayed to two decimal places.

Upvotes: 3

Saumini Navaratnam
Saumini Navaratnam

Reputation: 8850

As of v1.2.0

The format happens in nova/resources/js/components/Metrics/Base/ValueMetric.vue

formattedValue() {
    if (!this.isNullValue) {
        const numeralValue = numeral(this.value)

        return numeralValue.value() > 1000
            ? this.prefix + numeralValue.format('(0.00a)')
            : this.prefix + this.value
    }

    return ''
},

It is not configurable.

Workaround

You can edit above mentioned file just to return non format value. Then run npm run prod to build & run php artisan nova:publish command to copy updated files.

Note - Your changes will get override when you update Nova version in future.

Upvotes: 1

Related Questions