使用 Laravel Nova Indicator Field 使文本和自定义字段可排序?

Jan*_*Jan 6 php sorting laravel laravel-nova

我想让我的文本和自定义字段可排序。但我还没有找到一页可以解释它是如何工作的。

要么是这些:

Text::make('Type', function () {
    return $this->productType->name;
})
    ->sortable(),
Run Code Online (Sandbox Code Playgroud)

或者这是有效的:

Text::make('Type', function () {
    return $this->productType->name;
})
    ->sortable(function () {
        return $this->productType->name;
    }),
Run Code Online (Sandbox Code Playgroud)

你们知道如何使这些文本字段可排序吗?另外,是否可以使自定义字段像这样可排序?

Indicator::make('Status', function() {
    return $this->postStatus->status;
}),
Run Code Online (Sandbox Code Playgroud)

这是来自这个包: https: //github.com/inspheric/nova-indicator-field

亲切的问候和感谢!

小智 1

您可以indexQuery在资源(Laravel Nova 模型)中重新定义方法并加载要在索引页面中进行排序的字段

public static function indexQuery(NovaRequest $request, $query)
{
    return $query->addSelect([
        'name' => ProductType::select('name')
            ->whereColumn('product_id', 'products.id')
            ->limit(1),
    ]);
}
Run Code Online (Sandbox Code Playgroud)

->whereColumn('product_id', 'products.id'),语句更改取决于主要实体(在本例中 Product hasMany ProductType))

在 Laravel Nova 4 中,您可以使用徽章字段来显示状态laravel nova 徽章字段文档

Fields\Badge::make(__('Status'), 'status')
   ->textAlign('left')
   ->labels([
     'closed' => 'Closed',
     'canceled' => 'Canceled',
   ])
   ->map([
     'closed' => 'success',
     'canceled' => 'danger',
   ])
   ->withIcons()
   ->sortable(),
Run Code Online (Sandbox Code Playgroud)

这个徽章看起来很好看

徽章是什么样子的