Laravel Nova 中是否有一种方法允许对由函数调用填充的列进行排序?

问题描述 投票:0回答:0

我试图让 Nova Lens 中的填充列可以排序。填充我的 Number 字段的根数据来自镜头初始化期间调用的函数,用于访问 API。

出于某种原因,我可以对列值进行填充或排序,但不能同时进行。知道我错过了什么吗?

此代码有效(所有值均已填充)但不可排序:

Number::make('Months Remaining', function () { 
    return $this->foo('timeRemainingInMonths');
})
    ->sortable()
    ->onlyOnIndex(),

此代码有效(所有值均已填充)但不可排序:

Number::make('Months Remaining', function () { 
    return $this->foo('timeRemainingInMonths');
})
    ->sortable(function ($query, $direction) {
        $query->orderBy(
'timeRemainingInMonths', $direction);
    })
    ->onlyOnIndex(),

这段代码抛出一个警告,说

Argument 1 passed to sortable is expected to be type bool, string given
,但仍然会导致所有值都被填充,但不可排序:

Number::make('Months Remaining', function () { 
    return $this->foo('timeRemainingInMonths');
}) 
    ->sortable('timeRemainingInMonths')
    ->onlyOnIndex();

以上三种尝试的结果:

此代码可排序但不填充任何值:

Number::make('Months Remaining', 'timeRemainingInMonths')
    ->sortable()
    ->onlyOnIndex(), 

以上一次尝试的结果:

foo
函数已在本文中重命名,但这里是该函数的内容:

private function foo($field)
{
    $lotNo = $this->lotNo()->first()->lot_no;
    foreach (self::$bar as $mixtureLot) {
        if ($mixtureLot['lotNo'] == $lotNo) {
            return $mixtureLot[$field];
        }
    }
    return 'N/A';
}

bar
变量将始终是从 API 调用填充的数组。如果它有数据,则数组中的每一项都将采用以下形式

[
    'lotNo' => <string>,
    'qtyRemaining' => <integer>,
    'binIds' => <array of strings>,
    'manufactureDate' => <string>,
    'expirationDate' => <string>, 
    'timeRemainingInDays' => <integer>,
    'timeRemainingInMonths' => <integer>,
];
php laravel laravel-nova
© www.soinside.com 2019 - 2024. All rights reserved.