JSON format field processing

Model form provides the following components to process fields in JSON format, which is convenient for processing objects in the format of JOSN, one-dimensional arrays, two-dimensional arrays and other objects.

Before using the following components, must first set the cast of this field in the model

class Foo extends Model
    protected $casts = [
        'column_name' =>'json',

Key-value objects


If your field stores the {"field":"value"} format , you can use the keyValue component:


// Set verification rules

Fixed key-value object


Used to process JSON type field data of mysql or object type data of mongodb, and also store data values ​​of multiple fields in the form of JSON string in mysql string type In the field

Applicable to JSON type fields with fixed key values

$form->embeds('column_name', function ($form) {



// Custom title
$form->embeds('column_name','Field title', function ($form) {

The method call inside the callback function to construct the form element is the same as outside.

One-dimensional array


If your field is used to store a one-dimensional array in the format [[foo], "Bar"], you can use the list component:


// Set verification rules

// Set the maximum and minimum number of elements

Two-dimensional array

since v.16.13


If a field stores a two-dimensional array in json format, you can use the table form field:

$form->table('column_name', function ($table) {

At the same time, you need to set the accessor and modifier for this field in the model:

    public function getColumnNameAttribute($value)
        return array_values(json_decode($value, true) ?: []);

    public function setColumnNameAttribute($value)
        $this->attributes['column_name'] = json_encode(array_values($value));

This component is similar to the hasMany component, but it is used to handle the case of a single field and is suitable for simple two-dimensional data.