Skip to content
Allows you to group Nova fields and merge their output into a single JSON column
PHP Vue JavaScript
Branch: master
Clone or download
Latest commit b3b7e61 Nov 16, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist initial release Oct 15, 2019
resources/js initial release Oct 15, 2019
src fix issue #1 and #2 Nov 16, 2019
.gitignore initial release Oct 15, 2019
LICENSE initial release Oct 15, 2019
README.md initial release Oct 15, 2019
composer.json fix package name Oct 15, 2019
package.json initial release Oct 15, 2019
webpack.mix.js initial release Oct 15, 2019

README.md

Nova Json Wrapper

Latest Version on Packagist Total Downloads License

This field allows you to group Nova fields and merge their output into a single JSON column.

Installation

You can install the package via composer:

composer require digital-creative/nova-json-wrapper

Usage

Firstly you will need to update your model to cast the value of your attribute to an array:

class User extends Model
{
    protected $casts = [
        'options' => 'array'
    ];
}

Then create a JsonWrapper field within your nova resource and use the HasJsonWrapper trait.


use DigitalCreative/JsonWrapper/JsonWrapper;
use DigitalCreative/JsonWrapper/HasJsonWrapper;

class User extends Resource
{
    use HasJsonWrapper; // Important!

    public function fields(Request $request)
    {
        //...
        JsonWrapper::make('options', [
        
            Text::make('First Name')->rules('required'),
            Text::make('Last Name')->rules('required'),
        
            JsonWrapper::make('body_mass', [
        
                Text::make('Weight')->rules('required'),
                Text::make('Height')->rules('required'),
               
            ])
        
        ])
    }

}

This converts to

{ "first_name": "John", "last_name": "Doe", "body_mass": { "weight": 70, "height": 180 } }

and saves to the options column on the database.

Notes

There are no visual indications that the field is wrapped within a json, it's intentional. It was designed to work in condition with Conditional Container allowing to seamlessly create complex data structure and having it all saved in a single json column into your database, here is an full example:

public function fields(Request $request)
{
    Select::make('Type')
          ->options([
              1, 2, 3, 4, 5
          ])
          ->rules('required'),
    
    ConditionalContainer::make([
    
        JsonWrapper::make('data', [
    
            Text::make('First Name')->rules('required'),
            Text::make('Last Name')->rules('required'),
    
            Select::make('Gender')
                  ->options([
                      'male' => 'Male',
                      'female' => 'Female'
                  ])
                  ->rules('required'),
    
            ConditionalContainer::make([ JsonWrapper::make('extra', [ ... ]) ])->if('gender === male'),
            ConditionalContainer::make([ JsonWrapper::make('extra', [ ... ]) ])->if('gender === female'),
    
        ])
    
    ])->if('type >= 2'),
}

License

The MIT License (MIT). Please see License File for more information.

You can’t perform that action at this time.