GithubHelp home page GithubHelp logo

json's Introduction

json

A laravel nova field

Table of Contents

Install

composer require armincms/json

Usage

So simple.


  use Armincms\Json\Json;  
  

  Json::make("ColumnName", [ 
      Select::make(__("Discount Type"), "type")
          ->options([
              'percent' => __('Percent'),
              'amount' => __('Amount'),
          ])->rules('required')->default('percent'),
      Number::make(__("Discount Value"), "value")
          ->rules("min:0")
          ->withMeta([
              'min' => 0
          ]),   
  ]),

Nested Usage

Storing nested data is very like straight data. just like the following; use the Json nested.

  use Armincms\Json\Json;  
  

  Json::make("ColumnName", [ 
      Select::make(__("Discount Type"), "type")
          ->options([
              'percent' => __('Percent'),
              'amount' => __('Amount'),
          ])->rules('required')->default('percent'),
      Number::make(__("Discount Value"), "value")
          ->rules("min:0")
          ->withMeta([
              'min' => 0
          ]),   
      // nested data
      Json::make("discount", [ 
        Select::make(__("Discount Type"), "type")
            ->options([
                'percent' => __('Percent'),
                'amount' => __('Amount'),
            ])->rules('required')->default('percent'),
        Number::make(__("Discount Value"), "value")
            ->rules("min:0")
            ->withMeta([
                'min' => 0
            ]),   
      ]),
  ]),

Action Usage

It is possible to use the Json in the Action like follow:

use Armincms\Json\Json;

class UpdateTime extends Action
{
    use InteractsWithQueue, Queueable, SerializesModels; 


    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
      //
    }


    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return collect([
            /// some fields
            
            Json::make(mb_strtolower($meal), [
                Text::make(__("From"), 'from')->rules('required'),
                Text::make(__("Until"), 'until')->rules('required'),  
                Json::make(mb_strtolower($meal), [
                    Text::make(__("From"), 'from'),
                    Text::make(__("Until"), 'until'),  
                ]),
            ]),

            /// some fields
        ])->map(function($field) {
            return $field instanceof Json ? $field->fields() : [$field];
        })->flatten()->all();
    }
}


Showing And Hiding Fields

you can use the field show/hide methods on the JSON field. so this method will be called on each field under the Json field.The following example will hide all fields from the index view.

  use Armincms\Json\Json;  
  

  Json::make("ColumnName", [ 
       // fields
  ])->hideFromIndex(),

Save Last Values

By default; we clean the last data for store new data. but, it's possible to save the last data. for this, call the saveHistory method on parent Json class. this causes us to overwrite the new data without clean the last data. see the follow:

  use Armincms\Json\Json;  
  

  Json::make("ColumnName", [ 
       // fields
  ])->saveHistory(),

Separated Data

If you want store fields in one column but show in a separate place; you should make multiple Json field by one name.see the following:

  use Armincms\Json\Json;  
  

  Json::make("ColumnName", [ 
       // fields group 1
  ]),

  // other feilds


  Json::make("ColumnName", [ 
       // fields group 2
  ])->saveHistory(),

  • ATTENTION: at this situation, you should use saveHistory for next Json field.

Fill The Value

if you want to store the customized value of the field; you can use the fillUsing method and return custom value. see the follow:

  • fillUsing accept three argumnets $request, $attribute, $requestAttribute.
  use Armincms\Json\Json;  
  

  Json::make("ColumnName", [ 
       Number::make(__("Discount Value"), "value")
            ->rules("min:0")
            ->withMeta([
                'min' => 0
            ])->fillUsing(function($request, $attribute, $requestAttribute) {
                if($request->exists($requestAttribute)) { 
                    return $request[$requestAttribute];
                }

                return 1000;
            }), 
  ]),
  

Null Values

If there need to store some values as the null; you can use the nullable method that works like the Nova nullable. By default; nullable has the true value which means all values will be stored. But; It's possible to reject the storing of null values via passing the false value into the nullable method.

Auto Casting

If not defined JSON casting for the field attribute; we will convert the field Value into JSON. if you need disable this feature; use the ignoreCasting method;

About Implementation

Maybe there exists a question about how this package works?

I Should say that; this package doesn't have any corresponds component to the Vuejs. this package just uses callback's for data storing. so; won't changed any field.

with this implementation, you have access to your original fields without changes. So; for interacts with other packages or fields, exists toArray method to access to defined fields.

json's People

Contributors

zareismail avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

arzola u12206050

json's Issues

Save data but now show data

Laravel: 7.13.0
Nova: 3.6.0

Hi.

I'm testing your package.

Simple test not work.

            Json::make('data', [
               Select::make('Provider name', 'provider_name')
                ->options([
                    'castris' => 'Castris',
                    'ovh' => 'OVH',
                    'scaleway' => 'Scaleyway'
                ])->rules('required')->default('ovh'),
            ])->saveHistory()->hideFromIndex(),

This save data in data column, but not show when try to See or Edit a row.

Also if remove hidefromIndex() not show value.

But in database is correct. data has saved value.

Screenshot_20200602_100218

question

do you have maybe images of the package and how it works plz?

Date/datetime is not supported

If I use date/datetime Nova returns: "Date field must cast to 'date' in Eloquent model.
Is there a way how to handle those field types.

Json-Field doesn't work with Panels

you can simplify your code like following:

         new Panel('Fahrzeug-Provider-Details',  Json::make('vdp_data', [
                    Select::make('Aktualisierungsintervall', 'update_interval')->options([
                        'everyMinute'           => 'Jede Minute',
                        'everyFiveMinutes'      => 'Alle 5 Minuten',
                        'everyTenMinutes'       => 'Alle 10 Minuten',
                        'everyFifteenMinutes'   => 'Alle 15 Minuten',
                        'everyThirtyMinutes'    => 'Alle 30 Minuten',
                        'hourly'                => 'Stündlich',
                        'daily'                 => 'Täglich',
                        'weekly'                => 'Wöchentlich'
                    ]),

                    Text::make('Kundennummer', 'value'), 
            ])->hideFromIndex()->toArray()),

Doesn't work on encrypted fields

Hi, excellent field thanks.

However, with a small update, it works with encrypted fields too:

protected function isJsonCastable($model)
{
    return $model->hasCast($this->name, ['array', 'json', 'object', 'collection', 'encrypted:array', 'encrypted:json', 'encrypted:object', 'encrypted:collection']);
}

Bug when saving

Hello,

Nice plugin but I have a problem when i want to save multi fields :

 Json::make("Attributes2", [ 
                Text::make(__('field1')),
                Text::make(__('field2')),
                Text::make(__('field3')),
            ]),

Form result :
Capture d’écran 2020-03-06 à 22 48 50

When saving in database field:

{"0":"{\"0\":\"{\\\"field1\\\":\\\"value1\\\"}\",\"field2\":\"value2\"}","field3":"value3"}

I'm using Laravel 6 and Nova 2.10.

Thanks for help.
Bye.

ignoreCasting does not exist

The function ignoreCasting does not exist in the latest release v0.5.1

Create a new release containing it please.

Array json data not working

I have data in this format in db

[{"variation":"demo","quantity":"1","image":"abcdpic.jpg"},{"variation":"demo2","quantity":"5","image":"kkabcdpic.jpg"}]

trying to render it like

Row1        variation     demo
            quantity      1
            image         abcdpic.jpg
Row2        variation     demo2
            quantity      2
            image         kkabcdpic.jpg

but its not working

BooleanGroup Field is not loading values correctly

Laravel 8+
Nova 3.25

\Armincms\Json\Json::make('options', [
    BooleanGroup::make('Options')
    ->options( $this->getOptions() )
])->nullable()

This code will produce a JSON structure like so on save:

{"options":"{\"1\":false,\"2\":true,\"3\":true,\"4\":false,\"5\":false,\"6\":false,\"7\":false,\"8\":false,\"9\":false,\"10\":false}"}

However, when returning to the edit view, all values are selected as seen below:

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.