Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bernardev's avatar

Store multiple fields in JSON column (Nova CMS)

I have a posts table that has a json column "read_more_section".

And I want to store on that column the information regarding some fields on the NOVA CMS (title, description, label and link). Do you know how to properly do that in Nova? To store what is entered in these 4 fields in the json column "read_more_section"?

 public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')->sortable(),

                new Panel('Read More Section', [                

                    Text::make('Title')->rules(['max:255']),
                    Text::make('Description'),
                    Text::make('Label'),
                    Text::make('Link'),

                    
                ]),

               
        ];
    }
0 likes
1 reply
Andrei Nastasa's avatar

I needed something similar, so this is how you can try to do it:

Inside the Panel code:

new Panel('Read More Section', [                
    Text::make('Title')->rules(['max:255'])
	->resolveUsing(function($value, $resource, $attribute) use($request) {
        return Arr::get($resource->read_more_section, 'title');
    }),
    ......
]),

Then you can create a hidden field corresponding to the column name, and use the fields to fill in the data via fillUsing method:

Hidden::make('Read More Section')->fillUsing(function ($request, $model, $attribute, $requestAttribute) {
    $data = [
        'title' => $request->input('title'),
        'description' => $request->input('description'),
        'label' => $request->input('label'),
        'link' => $request->input('link'),
    ];
    
    $model->{$attribute} = $data;
})

You also need to add this method to your Nova Resource:

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 attributes do not have relevant columns in model table
    unset($modelObject->title);
    unset($modelObject->description);
    unset($modelObject->label);
    unset($modelObject->link);

    return $fillFields;
}
1 like

Please or to participate in this conversation.