Haymanot Brhane
Haymanot Brhane

Reputation: 21

Repeatable Field in laravel backpack

How to add repeatable field to add more than one phone number and address with Add Phone, Add Address option using laravel backpack?

i have 3 tables (person, phone, Address tables)

address and phone tables has relationship with person tables

i want to accept name using text field, accept phone numbers and adresses using repeatable field and save name in person table, phone numbers in phone table and adresses address table separately.

     $this->crud->addField([   
        'name'  => 'Landline',
        'label' => 'Phone Numbers',
        'type'  => 'repeatable',
        'fields' => [
            [
                'name'    => 'Landline',
                'type'    => 'text',
                'label'   => 'Phone Number',
                'wrapper' => ['class' => 'form-group col-md-12'],
            ],
       ,
        ],
        'new_item_label'  => '+Add Phone', 

      ]);
   

      $this->crud->addField([   
        'name'  => 'person_addresses', // belongsTo relation
        'label' => 'Adresses',
        'type'  => 'repeatable',
        'pivot' => true,
        'fields' => [
            [
              'label' => 'Region',
              'type' => 'select',
              'name' => 'region_id',
              'attribute' =>'region', 
              'entity' => 'region',
              'wrapper'   => [ 
              'class' => 'form-group col-md-4'],
            ],
            [
              'label' => 'City/Subcity',
              'type' => 'select',
              'name' => 'city_id',
              'attribute' =>'city', 
              'entity' => 'city', 
              'allows_null' => true,
              'model' => 'App\Models\city',
              'wrapper'   => [ 
              'class' => 'form-group col-md-4'],
          ],
          [
            'name' => 'location_pin',
            'type'=> 'address_algolia',
            'label' => "Location Pin",
            'wrapper'   => [ 
              'class' => 'form-group col-md-4'
            ],
        ],
          
        ],
        'new_item_label'  => 'Add Address', 
      ]);
          

Upvotes: 1

Views: 2852

Answers (1)

tabacitu
tabacitu

Reputation: 6193

By default the repeatable field will store that info as JSON in the database.

If you want it stored another way, I recommend you create the field using a bogus name (eg contact), then

  • create a mutator in your model (eg setContactAttribute($value)) that will parse that array and store it in the proper tables/relationships;
  • create an accessor in your model (eg getContactAttribute) that will get the information from the relationships and return it in the same array format;

Upvotes: 3

Related Questions