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/ */
// 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'),
]);
]
}
// 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']]];