Laravel Dynamic Fields - Forms

Asked

Viewed 275 times

-1

How best to create dynamic fields in the view ?

Suppose I need to create several products in the register and mention the type of product or category.

I’m using it this way:


       <form action="" method="POST" enctype="multipart/form-data">
     <div class="form-group col-md-6">
          <div class="input-group">
              <input type="produto" class="form-control" value="" id="produto" name="produto[]"
                  placeholder="Nome do Produto">

   <select class="form-control select2" name="categoria[]" style="width: 100%;">
                  <option value="" selected="">Selecione</option>
                  <option value="1">Categoria 1</option>
                  <option value="2">Categoria 2</option>
              </select>
          </div>
      </div>
      <button type="submit">Enviar</button>
    </form>

I’m having trouble capturing this in Controller ? I need to "match" the data of each product with the chosen category.

Does anyone have an example ?

  • 1

    Qual a melhor forma de criar campos dinâmicos na View? A: the answer is a reactive framework (angular, vue or reactjs)

  • Only with HTML5 ?

1 answer

0

With Blade from Laravel you can use @foreach to list in select as follows:

<select name='categoria'>
   <option value="" disable>Selecione uma categoria</option>
   @foreach($categoria as $categorias)
     <option value="{{$categorias->id}}">{{$categorias->name}}</option>
   @endforeach
</select>

In the controller I am sending the data in the category variable and I list all category records for the select, the values passed back to do relationship is an ID if you use the name just change the value, and this way you list all categories in html using Blade. If you have any questions about sending controller data, let me know...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.