Laravel Nova:显示其他表的分区';s柱

Laravel Nova:显示其他表的分区';s柱,laravel,laravel-nova,Laravel,Laravel Nova,我使用Laravel Nova分区来显示每个类别的项目数量 public function calculate(Request $request) { return $this->count($request, Item::class, 'category_id'); } 这很好,但在屏幕上显示类别id 我宁愿显示类别名称 模型构建如下: class Item extends Model { public function category() {

我使用Laravel Nova分区来显示每个类别的项目数量

public function calculate(Request $request) {
   return $this->count($request, Item::class, 'category_id');
}
这很好,但在屏幕上显示类别id

我宁愿显示类别名称

模型构建如下:

class Item extends Model 
{
    public function category()
    {
        return $this->belongsTo('App\Category');
    }
 }

此外,我在Nova类别资源中定义了以下内容:

public static $title = 'category_name';
如何显示类别名称而不是类别id?

通常,将分区度量划分为 组将是简单的键,而不是“人”的东西 友好的”。或者,如果要显示按 如果列为布尔值,Nova将把组标签显示为“0” 及"1。因此,Nova允许您提供一个 将标签格式化为更可读的格式:

所以,在你的情况下,它会是这样的:

/**
 * Calculate the value of the metric.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return mixed
 */
public function calculate(Request $request)
{
    return $this->count($request, Item::class, 'category_id');
        ->label(function ($value) {
            // Get your category name here
            // For example: return \App\Category::find($value)->name; 
        });
}

这是我已经做过的(作为其他事情的一部分),但不幸的是,这没有显示名称(在分区中)。啊,对了,编辑了我的答案,以备可能的实现。Super。您的建议有效,但是您需要添加return语句。所以它将是“returncategory::find($value)->Category_name;”。建议你修改答案,我会接受的。谢谢你,伙计!
/**
 * Calculate the value of the metric.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return mixed
 */
public function calculate(Request $request)
{
    return $this->count($request, User::class, 'stripe_plan')
            ->label(function ($value) {
                switch ($value) {
                    case null:
                        return 'None';
                    default:
                        return ucfirst($value);
                }
            });
}
/**
 * Calculate the value of the metric.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return mixed
 */
public function calculate(Request $request)
{
    return $this->count($request, Item::class, 'category_id');
        ->label(function ($value) {
            // Get your category name here
            // For example: return \App\Category::find($value)->name; 
        });
}