Laravel Nova extra 'images' field on forms without actual database column

Issue

Creating extra field ‘images’ resource forms usually throws ‘column not found‘ type database level error.
But I need that type of extra field on the resource forms for some business logic under the hood when the create/update form is submitted.

I tried using removeNonCreationFields method on resource to remove that field column from saving to database but does not work and still throws error.

Please note that ->hideWhenCreating() or ->readonly() is not relevant as I need to interact on that field on create/delete forms.

Is there any other way to make such situation success with extra fields? Please help. Thanks.

Solution

My solution was:

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;
}

Then you should use two functions, one for how to save in database and another for how to retrieve that specific data from database. Use these on the extra Field.

->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);
}),

Hoping this will solve your issue. Thanks

Answered By – Hasnat Babur

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published