title: Laravel duplicate all fields of form using blade and insert multiple rows from a single query using eloquent
body: <p>I want to have a add another form button that will duplicate all the fields of my form but will still remain one submit button and pass all the data of that into the controller to insert it using one single query.</p>
  
  <p>Here is the <strong>create.blade.php</strong> that opens the form</p>
  
  <pre><code>@extends('encoder-dashboard.layouts.app')
  
  @section('css')
    &lt;link rel="stylesheet" href="/css/datepicker.css"&gt;
  @endsection
  
  @section('content')
      &lt;section class="content-header"&gt;
          &lt;h1&gt;
              Add New Analysis Request
          &lt;/h1&gt;
      &lt;/section&gt;
      &lt;div class="content"&gt;
          @include('adminlte-templates::common.errors')
          &lt;div class="box box-primary"&gt;
  
              &lt;div class="box-body"&gt;
                  &lt;div class="row"&gt;
                      {!! Form::open(['route' =&gt; 'encoder.analysis-request.store']) !!}
  
                          @include('encoder-dashboard.analysis-request.fields')
  
                      {!! Form::close() !!}
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  @endsection
  
  @section('scripts')
    &lt;script src="/js/datepicker.js"&gt;&lt;/script&gt;
  
    &lt;script&gt;
    $('#datepicker').datepicker({
        autoclose: true
      });
    &lt;/script&gt;
  @endsection
  </code></pre>
  
  <p>And here are the fields listed in <strong>fields.blade.php</strong></p>
  
  <pre><code>&lt;!-- Client Id Field --&gt;
  &lt;div class="form-group col-sm-6"&gt;
      {!! Form::label('client_id', 'Client Name:') !!}
       {!! Form::select('client_id[]', $client, null, ['class' =&gt; 'form-control','required'])!!}
  &lt;/div&gt;
  
  &lt;!-- Password Field --&gt;
  &lt;div class="form-group col-sm-6"&gt;
      {!! Form::label('sample_code', 'Sample Code:') !!}
      {!! Form::text('sample_code[]', null, ['class' =&gt; 'form-control','required']) !!}
  &lt;/div&gt;
  
  &lt;!-- Password Field --&gt;
  &lt;div class="form-group col-sm-6"&gt;
      {!! Form::label('sample_description', 'Description:') !!}
      {!! Form::text('sample_description[]', null, ['class' =&gt; 'form-control','required']) !!}
  &lt;/div&gt;
  
  &lt;!-- Password Field --&gt;
  &lt;div class="form-group col-sm-6"&gt;
      {!! Form::label('quantity', 'Quantity:') !!}
      {!! Form::number('quantity[]', null, ['class' =&gt; 'form-control','required']) !!}
  &lt;/div&gt;
  
  &lt;!-- Password Field --&gt;
  &lt;div class="form-group col-sm-6"&gt;
      {!! Form::label('analysis_requested', 'Analysis Request:') !!}
      {!! Form::text('analysis_requested[]', null, ['class' =&gt; 'form-control','required']) !!}
  &lt;/div&gt;
  
  &lt;!-- Password Field --&gt;
  &lt;div class="form-group col-sm-6"&gt;
      {!! Form::label('special_instruction', 'Special Instruction:') !!}
      {!! Form::text('special_instruction[]', null, ['class' =&gt; 'form-control','required']) !!}
  &lt;/div&gt;
  
  &lt;!-- Submit Field --&gt;
  &lt;div class="form-group col-sm-12"&gt;
      {!! Form::submit('Save', ['class' =&gt; 'btn btn-success', 'onClick' =&gt; 'this.form.submit(); this.disabled=true; this.value="Saving…";']) !!}
      &lt;a href="{!! route('encoder.analysis-request.index') !!}" class="btn btn-default"&gt;Cancel&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>All the fields above from the form will be duplicated and pass it as an array right? How do I do that maybe using jQuery or simple vanila JS as well?</p>
  
  <p>and if I pass this data it would look like these.</p>
  
  <pre><code>array:8 [▼
    "_token" =&gt; "xEGtr4h0f4gim0sLvABZivvq36UNaRHWJ08PMWTI"
    "client_id" =&gt; array:1 [▼
      0 =&gt; "1"
    ]
    "sample_code" =&gt; array:1 [▼
      0 =&gt; "eqwewqeqewqrwereqwe"
    ]
    "sample_description" =&gt; array:1 [▼
      0 =&gt; "dasdsadsadsadasd"
    ]
    "quantity" =&gt; array:1 [▼
      0 =&gt; "232"
    ]
    "analysis_requested" =&gt; array:1 [▼
      0 =&gt; "dsadsadsadsadsad"
    ]
    "special_instruction" =&gt; array:1 [▼
      0 =&gt; "dwqewqewqewqewqeq"
    ]
  ]
  </code></pre>
  
  <p>This is my query below when inserting one single record.</p>
  
  <pre><code>$analysis_request = $this-&gt;analysisrequestRepository-&gt;create([
              'client_id' =&gt; $input['client_id'],
              'sample_code' =&gt; $input['sample_code'],
              'sample_description' =&gt; $input['sample_description'],
              'quantity' =&gt; $input['quantity'],
              'analysis_requested' =&gt; $input['analysis_requested'],
              'special_instruction' =&gt; $input['special_instruction'],
              'status' =&gt; 'for_testing'
          ]);
  
          $id = $analysis_request-&gt;id;
  
          $request_actors = RequestActors::create([
              'request_id' =&gt; $id,
              'encoder_id' =&gt; Auth::guard('encoder')-&gt;user()-&gt;id,
              'microbiologist_id' =&gt; null
          ]);
  </code></pre>
  
  <p>this is my query above for inserting one single record. I also performed another query to insert the id of the request which has a relationship to the analysis request that is inserted in the first query.</p>
  
  <p>Now if I will make it to insert many I will do an array inside an array and insert it each record right? Like this.</p>
  
  <pre><code>$data = array(
          array(
              'client_id' =&gt; $input['client_id'],
              'sample_code' =&gt; $input['sample_code'],
              'sample_description' =&gt; $input['sample_description'],
              'quantity' =&gt; $input['quantity'],
              'analysis_requested' =&gt; $input['analysis_requested'],
              'special_instruction' =&gt; $input['special_instruction'],
              'status' =&gt; 'for_testing'
          ),
          // and so on.
      );
  
          AnalysisRequest::insert($data);
  </code></pre>
  
  <p>But I had an error in the code above it says that <strong>Array to string conversion</strong></p>
  
  <p>Or will it be better if I do an for each?</p>
  
  <p>My only goal here is to perform an insert many query depends on the entries to add.</p>
  
  <p>Appreciate if someone can help.
  Thanks in advance.</p>