Laravelnova在字段的fillUsing回调中获取创建模型的ID值

Laravel nova getting the ID value of the created model in the fillUsing callback of the field

我正在使用 Laravel Nova 开发 Web 应用程序。 Laravel Nova 是一项相当新的技术。现在,我现在正在做的是覆盖 Field 回调以添加自己的业务逻辑,而不是执行数据库操作。请参阅下面的场景。

这是我的资源字段方法

public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make("Subject")->fillUsing(function(){
                //here, I like to get the id of the created model. How?
            }),
            Text::make('Title')->sortable()
        ];
    }

如您所见,我正在覆盖主题字段的逻辑。我喜欢在回调中获取模型的 ID。我怎样才能实现它?

当您查看 Field.php class 时,您会发现 fillCallback 的用法是这样的:

protected function fillAttribute(NovaRequest $request, $requestAttribute, $model, $attribute)
{
    if (isset($this->fillCallback)) {
        return call_user_func(
            $this->fillCallback, $request, $model, $attribute, $requestAttribute
        );
    }

    $this->fillAttributeFromRequest(
        $request, $requestAttribute, $model, $attribute
    );
}

因此,在回调函数中,您可以像这样访问变量:

Text::make('Subject')->fillUsing(function($request, $model, $attribute, $requestAttribute) {
    dd($model->id);
});