我正在使用Laravel Nova开发一个Web应用程序。Laravel Nova是相当新的。我现在遇到了数据库关系和字段的问题。我喜欢忽略数据库操作中的字段。这就是我的场景。
在作业资源中,我有以下字段方法
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name', 'name'),
Text::make('Email', 'email'),
Select::make('Contract Types')->options($array_of_options)//I want to ignore this field
];
}
如您所见,最后一个字段是合同类型。
当我从仪表板创建新作业时,它抛出错误,因为作业模型上没有contract_types列。我喜欢忽略数据库操作中的那个字段。我怎么才能得到它呢?
发布于 2018-10-30 10:36:13
根据docs https://nova.laravel.com/docs/1.0/resources/fields.html#showing-hiding-fields
Select::make('Contract Types')
->options($array_of_options)
->hideWhenCreating()
发布于 2020-04-22 08:48:13
公认的答案并不完全正确。它可以防止将值存储在数据库中,但也会完全隐藏表单中的字段。在某些奇怪的情况下,您可能想要显示一个未存储的字段。
我的建议是将以下内容添加到资源中(或者,如果您希望在多个资源中使用,则将其放入更可重用的位置):
public static function fill(NovaRequest $request, $model)
{
return static::fillFields(
$request, $model,
(new static($model))->creationFieldsWithoutReadonly($request)->reject(function ($field) use ($request) {
return in_array('ignoreOnSaving', $field->meta);
})
);
}
在相关字段中,您可以添加:
->withMeta(['ignoreOnSaving'])
这将为您提供一个字段来填充,而无需将其保存在模型中。
发布于 2020-10-15 18:52:56
您可以自定义处理字段数据,只需使用Field类的fillUsing()
方法即可。一个例子
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name', 'name'),
Text::make('Email', 'email'),
Select::make('Contract Types', 'unique_key_for_model')
->options($array_of_options)
->fillUsing(function(NovaRequest $request, $model, $attribute, $requestAttribute) {
/*
$request->input('unique_key_for_model') // Value of the field
$model->unique_key_for_model // DOES NOT exists, so no errors happens
*/
// or just return null;
return null;
}),
];
}
https://stackoverflow.com/questions/52095827
复制相似问题