PHP code example of hmayer / json-field

1. Go to this page and download the library: Download hmayer/json-field library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hmayer / json-field example snippets


JSON::make('Author', [
    Text::make('Name')->rules(['string', '

// prequesite: the 'author' attribute needs to casted into a json castable type
// e.g. object, array, ...
['author' => ['name' => '', 'email' => '']]

use Hmayer\JsonField\JSON;

// within your nova resource
public function fields()
{
    return [
        //...
        JSON::make('Some Json Column Name', [
            Text::make('First Field'),
            Text::make('Second Field'),
            Text::make('Third Field'),
        ]);
    ]
}

JSON::make('Address', 'address', [
    Text::make('Street')->fillUsing(fn ($request, $model, $attribute, $requestAttribute) => $request[$requestAttribute] . ' Foo'),
]);

JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->fillAtOnce();

// given these fields:
JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->fillAtOnce(function ($request, $requestValues, $model, $attribute, $requestAttribute) {
    return ['nested' => $requestValues];
});

// and a request with ['address->street' => 'test str. 5', 'address->city' => 'test city']

// we will get
$requestValues = ['street' => 'test str. 5', 'city' => 'test city'];

// which will be pased into the fillAtOnce callback leading to the following in our db:
['address' => ['nested' => ['street' => 'test str. 5', 'city' => 'test city']]];

JSON::make('Address', 'address', [
    Text::make('Street'),
    Text::make('City'),
])->nullable()->nullValues(['_', 0])

JSON::make('Meta', [
    Text::make('Street'),

    JSON::make('Location', [
        Text::make('Latitude'),
        Text::make('Longitude'),
    ]),
]);

new Panel('Brand Settings', 
    JSON::make('brand_settings', [
        Image::make('Logo')->disk('public'),
        Color::make('Primary Color')->swatches(),
        Color::make('Secondary Color')->swatches(),
    ]),
),

new Panel('Brand Settings', [
    Text::make('Some Field'),
    JSON::make('brand_settings', [
        Image::make('Logo')->disk('public'),
        Color::make('Primary Color')->swatches(),
        Color::make('Secondary Color')->swatches(),
    ]),
]),