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

Torpedo's avatar

Nova Metrics simple count doesn't work

If i count my User::class model, the metrics work, but when i change it to an other model it shows

" 0 No Data "

the model exists an i do import it.. is it because i imported the data for the model directly in phpmyadmin? if so.. what can i do?

thanks in advance.

0 likes
7 replies
Torpedo's avatar

<?php


namespace App\Nova\Metrics;

use App\Kunde;
use Illuminate\Http\Request;
use Laravel\Nova\Metrics\Value;

class TotalKunden extends Value
{
    
    /**
      * Calculate the value of the metric.
      *
      *@param \Illuminate\Http\Request $request
      *@return mixed
      */
      
      public function calculate(Request $request)
      {
          return $this->count($request, Kunde::class);
      }
    
    


    /**
    * Get the ranges available for the metric.
    **/
    
    public function ranges()
    {
        return [
            30 => '30 Tage',
            60 => '60 Tage',
            365 => '1 Jahr',
            'MTD' => 'Seit Monatsbeginn',
            'QTD' => 'Seit Quartalbeginn',
            'YTD' => 'Seit Jahresanfang',
            
        ];
    }
}

code should be ok? but it doesn't work..

ejdelmonico's avatar

Do you have an associated eloquent model for the Nova resource? If not, you won't get the data. In other words, you create a Nova resource and associate it with a model such as App\Post with App\Nova\Post.

Torpedo's avatar
Torpedo
OP
Best Answer
Level 3

maybe someone has the same problem.. so the answer is:

metrics query is looking for the created_at column in the DB, so it must have an date

the metrics query is like this..

SELECT count(`id`) as aggregate FROM `Objects` WHERE `created_at` between '2018-11-05 12:51:22' and '2018-12-05 12:51:22' and `Objects`.`deleted_at` IS NULL

:)

2 likes
yvomenezes's avatar

Hi, @Torpedo! Thanks for this. Do you know if it is possible to change the default column from created_at to another one?

yvomenezes's avatar

I've found the answer to my own question. The Value's count method has a $dateColumn parameter to select the desired column for the range.

/**
 * Return a value result showing the growth of an count aggregate over time.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Illuminate\Database\Eloquent\Builder|string  $model
 * @param  string|null  $column
 * @param  string|null  $dateColumn
 * @return \Laravel\Nova\Metrics\ValueResult
 */
public function count($request, $model, $column = null, $dateColumn = null)
{
    return $this->aggregate($request, $model, 'count', $column, $dateColumn);
}
anderly's avatar

As @Torpedo mentioned above. You need dates on your model for the date range-based counts to work.

If you just want a simple count of records in the table for your model, then don't use the count method in your calculate method, use the result method instead.

Example:

    /**
     * Calculate the value of the metric.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    public function calculate(Request $request)
    {
        return $this->result(Kunde::count());
    }

Note: Ranges won't work for this. So, you can disable them by returning [] in your ranges method.

1 like

Please or to participate in this conversation.