Skip to content
Advertisement

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'),

                    
                ]),

               
        ];
    }

Advertisement

Answer

You could make a getter and setter, catch the values of these fields and convert it to a json object.

or you can do it with https://github.com/whitecube/nova-flexible-content and just set a limit of 1 in the layout.

https://whitecube.github.io/nova-flexible-content/#/?id=limiting-layouts-per-type https://whitecube.github.io/nova-flexible-content/#/?id=limiting-layouts

Nova resource

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

                new Panel(__('Read More'), [                

                    Flexible::make('Specifications', read_more_section)
                       ->addLayout(ReadMoreLayout::class)
                       ->limit(1),
                    
                ]),

               
        ];
    }

ReadMoreLayout.php

class ReadMoreLayout extends Layout
{
    /**
     * The layout's unique identifier
     *
     * @var string
     */
    protected $name = 'title';

    /**
     * The displayed title
     *
     * @var string
     */
    protected $title = 'Read More Section';

    protected $limit = 1;

    /**
     * Get the fields displayed by the layout.
     *
     * @return array
     */
    public function fields()
    {
        return [
                Text::make('Title')->rules(['max:255']),
                Text::make('Description'),
                Text::make('Label'),
                Text::make('Link'),
        ];
    }
}