在 Laravel nova 中自定义 Partition Metrics 的值

Customizing the value of Partition Metrics in Laravel nova

我有一个 table usersoperations 有关。

\App\user:

public function operation()
    {
        return $this->belongsTo('App\Operation');
    }

所以我能做到:

$operation = user->operation

现在我想在用户资源页面中添加一个分区指标,它告诉我按操作分组的用户数量

现在,它显示一个 id,它不过滤我的用户(需要用 company_id 过滤查询)

public function calculate(NovaRequest $request)
    {
        return $this->count($request, User::class, 'operation_id', 'name');
    }

我尝试添加 name 列名,但首先它不起作用,其次,我需要操作的名称,而不是用户,所以它应该类似于 operation.name 但它也不起作用。

如何打印操作名称而不是 operation_id

PD:基本上,整个过程更加痛苦,因为我无法在指标中看到 dump()dd() 的输出,所以我无法调试。

您应该使用 Laravel/nova 文档中提到的 ->label() 方法: https://nova.laravel.com/docs/3.0/metrics/defining-metrics.html#customizing-partition-labels.

那么,正确的代码应该是:

public function calculate(NovaRequest $request)
    {
        return $this->count($request, User::class, 'operation_id')
               ->label(function($operationId){
                 switch($operationId){
                       case 'opration_1_id': return 'operation_1_name';
                       break; 
                       case 'opration_2_id': return 'operation_2_name';
                       break; 
                       default: return 'Other';
                    }
               });
    }