Laravel Nova:显示其他 table 列的分区
Laravel Nova: Partition to display other table's column
我正在使用 Laravel Nova 分区来显示每个类别的项目数量。
public function calculate(Request $request) {
return $this->count($request, Item::class, 'category_id');
}
这工作正常,但当然会在屏幕上显示 category_id。
我宁愿显示category_name。
模型构建如下:
class Item extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
}
和
class Category extends Model
{
public function item()
{
return $this->hasMany('App\Item');
}
}
此外,我在 Nova 类别资源中定义了以下内容:
public static $title = 'category_name';
如何显示类别名称而不是类别 ID?
Often, the column values that divide your partition metrics into
groups will be simple keys, and not something that is "human
friendly". Or, if you are displaying a partition metric grouped by a
column that is a boolean, Nova will display your group labels as "0"
and "1". For this reason, Nova allows you to provide a Closure that
formats the label into something more readable:
/**
* 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;
});
}
我正在使用 Laravel Nova 分区来显示每个类别的项目数量。
public function calculate(Request $request) {
return $this->count($request, Item::class, 'category_id');
}
这工作正常,但当然会在屏幕上显示 category_id。
模型构建如下:
class Item extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
}
和
class Category extends Model
{
public function item()
{
return $this->hasMany('App\Item');
}
}
此外,我在 Nova 类别资源中定义了以下内容:
public static $title = 'category_name';
如何显示类别名称而不是类别 ID?
Often, the column values that divide your partition metrics into groups will be simple keys, and not something that is "human friendly". Or, if you are displaying a partition metric grouped by a column that is a boolean, Nova will display your group labels as "0" and "1". For this reason, Nova allows you to provide a Closure that formats the label into something more readable:
/**
* 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;
});
}