Laravel Nova extra 'images' 表单上的字段没有实际的数据库列
Laravel Nova extra 'images' field on forms without actual database column
创建额外字段 'images' 资源表单通常会抛出“未找到列”类型的数据库级错误。
但是当提交 create/update 表单时,我需要在资源表单上为某些业务逻辑添加这种类型的额外字段。
我尝试在资源上使用 removeNonCreationFields
方法从保存到数据库中删除该字段列,但不起作用并且仍然抛出错误。
请注意 ->hideWhenCreating() or ->readonly()
不相关,因为我需要在 create/delete 表单上的该字段上进行交互。
有没有其他方法可以通过额外的字段使这种情况成功?请帮忙。谢谢
我的解决方案是:
app/Nova/Post.php
/**
* @param NovaRequest $request
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Support\Collection $fields
* @return array|void
*/
protected static function fillFields(NovaRequest $request, $model, $fields)
{
$fillFields = parent::fillFields($request, $model, $fields);
// first element should be model object
$modelObject = $fillFields[0];
// remove all extra non-database attributes from the model
unset($modelObject->to_profile_gallery);
// I am not sure it will work if we unset on $model parameter and return it
// But you should try first doing so on $model parameter and return $model
return $fillFields;
}
那么你应该使用两个函数,一个用于如何保存在数据库中,另一个用于如何从数据库中检索特定数据。在额外的字段上使用这些。
->fillUsing(function($request, $model, $attribute, $requestAttribute){
// during creation photos are handled by Nova Resource Observer
if($model->type !== post_type_photo()) return;
// run only for update request
FilepondHelper::handleMediaFillUsingCallback(PostMediaTag::photos, true, $request, $model, $attribute, $requestAttribute); // only update
})
->resolveUsing(function($value, $resource, $attribute) use($request){
return FilepondHelper::handleMediaResolveUsingCallback(PostMediaTag::photos, $value, $resource, $attribute, $request);
}),
希望这能解决您的问题。谢谢
Hasnat 方法非常适合我的情况。
我希望在仅为内部逻辑创建资源时有一个特殊字段,它应该是 ignored/discarded 并且与数据库字段没有任何关系。
谢谢!
创建额外字段 'images' 资源表单通常会抛出“未找到列”类型的数据库级错误。 但是当提交 create/update 表单时,我需要在资源表单上为某些业务逻辑添加这种类型的额外字段。
我尝试在资源上使用 removeNonCreationFields
方法从保存到数据库中删除该字段列,但不起作用并且仍然抛出错误。
请注意 ->hideWhenCreating() or ->readonly()
不相关,因为我需要在 create/delete 表单上的该字段上进行交互。
有没有其他方法可以通过额外的字段使这种情况成功?请帮忙。谢谢
我的解决方案是:
app/Nova/Post.php
/**
* @param NovaRequest $request
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Support\Collection $fields
* @return array|void
*/
protected static function fillFields(NovaRequest $request, $model, $fields)
{
$fillFields = parent::fillFields($request, $model, $fields);
// first element should be model object
$modelObject = $fillFields[0];
// remove all extra non-database attributes from the model
unset($modelObject->to_profile_gallery);
// I am not sure it will work if we unset on $model parameter and return it
// But you should try first doing so on $model parameter and return $model
return $fillFields;
}
那么你应该使用两个函数,一个用于如何保存在数据库中,另一个用于如何从数据库中检索特定数据。在额外的字段上使用这些。
->fillUsing(function($request, $model, $attribute, $requestAttribute){
// during creation photos are handled by Nova Resource Observer
if($model->type !== post_type_photo()) return;
// run only for update request
FilepondHelper::handleMediaFillUsingCallback(PostMediaTag::photos, true, $request, $model, $attribute, $requestAttribute); // only update
})
->resolveUsing(function($value, $resource, $attribute) use($request){
return FilepondHelper::handleMediaResolveUsingCallback(PostMediaTag::photos, $value, $resource, $attribute, $request);
}),
希望这能解决您的问题。谢谢
Hasnat 方法非常适合我的情况。 我希望在仅为内部逻辑创建资源时有一个特殊字段,它应该是 ignored/discarded 并且与数据库字段没有任何关系。 谢谢!