https://github.com/dcasia/nova-json-wrapper
Allows you to group Nova fields and merge their output into a single JSON column
https://github.com/dcasia/nova-json-wrapper
json json-wrapper laravel laravel-nova-field nova
Last synced: about 2 months ago
JSON representation
Allows you to group Nova fields and merge their output into a single JSON column
- Host: GitHub
- URL: https://github.com/dcasia/nova-json-wrapper
- Owner: dcasia
- License: mit
- Created: 2019-10-15T01:21:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-25T06:44:57.000Z (7 months ago)
- Last Synced: 2025-04-09T19:18:43.512Z (about 2 months ago)
- Topics: json, json-wrapper, laravel, laravel-nova-field, nova
- Language: PHP
- Size: 289 KB
- Stars: 13
- Watchers: 2
- Forks: 10
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Nova Json Wrapper
[](https://packagist.org/packages/digital-creative/nova-json-wrapper)
[](https://packagist.org/packages/digital-creative/nova-json-wrapper)
[](https://github.com/dcasia/nova-json-wrapper/blob/master/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:
```php
class User extends Model
{
protected $casts = [
'options' => 'array'
];
}
```Then create a `JsonWrapper` field within your nova resource and use the `HasJsonWrapper` trait.
```php
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
```json
{ "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, this is intentional. It was designed to work
in condition with [Conditional Container](https://github.com/dcasia/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:```php
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](https://raw.githubusercontent.com/dcasia/nova-json-wrapper/master/LICENSE) for more information.