user14053977
user14053977

Reputation: 151

How to insert multiple lines into database from dynamic input fields using Laravel 8 / jQuery

I'm trying to insert many lines to database from dynamic input fields.

So I'm using the following code to generate dynamically the input fields :

$(document).ready(function() {

  $("body").on("click", ".add_new_frm_field_btn", function() {
    var random = 1 + Math.floor(Math.random() * 1000); //generate random values..
    var index = $(".form_field_outer").find(".form_field_outer_row").length + 1;
    //added data-index and outer..class
    $(".form_field_outer").append(`<div class="col-12 outer" data-index="${index}_${random}"><div class="card-body form_field_outer_row"> <div class="form-row"><div class="form-group col-md-4"> <label for="inputState">Casting</label><select id="id_casting" class="form-control" name="rows[${index}][id_casting]">
<option selected>Choose...</option><option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option> </select></div><div class="form-group col-md-4"><label for="inputState">Type de contrat</label><select id="id_modele_contrat" class="form-control" name="rows[${index}][id_modele_contrat]"> <option selected>Choose...</option><option>...</option> </select></div><div class="card-body "><button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button></div>
</div></div></div> `);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<div class="col-12">
  <div class="card mb-4 form_field_outer">
    <!--added outer and data-index-->
    <div class="card-body form_field_outer_row outer" data-index="0">
      <form>
        <div class="form-row">
          <div class="form-group col-md-4">
            <label for="inputState">Casting</label>
            <select id="id_casting" class="form-control" name="rows[0][id_casting]">
              <option selected>Choose...</option>
              <option data-id="{{$casting->id_casting}}" value="{{$casting->id_casting}}">{{$casting->nom.' '.$casting->prenom}}</option>
            </select>
          </div>
          <div class="form-group col-md-4">
            <label for="inputState">Type de contrat</label>
            <select id="id_modele_contrat" class="form-control" name="rows[0][id_modele_contrat]">
              <option selected>Choose...</option>
              <option>...</option>
            </select>
          </div>
          <div class="card-body ">
            <button type="button" class="btn btn-outline-warning mb-1 remove_node_btn_frm_field">Delete</button>
            <button type="button" class="btn btn-outline-warning mb-1 add_new_frm_field_btn">Add</button>
          </div>
        </div>
      </form>
    </div>

  </div>


</div>
<div>

And the following controller :

foreach($request->input('rows') as $key => $value) {

                      Projet_Casting::create([
                         'id_projet' =>  1,
                         'id_casting' => $request->id_casting[$key],
                         'id_contrat'  => 1,
    ]);

But only one line is inserted into databse , while I want to insert all the added input fields into database.

How can I get all the added lines and insert them into databse ?

is there something wrong in my jQuery code ?

UPDATE

When I did in my controller only :

dd($request->all());

I get :

"rows" => array:1 [
    0 => array:2 [
      "id_casting" => "Choose..."
      "id_modele_contrat" => "Choose..."
    ]
  ]

For example I'm adding two rows , but I' get only the first additional input field.

it should be in thos example

 "rows" => array:2 [

Upvotes: 1

Views: 805

Answers (2)

Fawaz Chughtai
Fawaz Chughtai

Reputation: 41

Its quite late but replying in hope that it might help others, place your

Form

tag out side

.form_field_outer div

Upvotes: 1

Rakesh kumar Oad
Rakesh kumar Oad

Reputation: 1336

$data = $request->input('rows');
dd($data[0]); // out put will be array:2 ["id_casting" => "Choose...""id_modele_contrat" => "Choose..."]

foreach($data[0] as $key => $value) {

                  Projet_Casting::create([
                     'id_projet' =>  1,
                     'id_casting' => $value,
                     'id_contrat'  => 1,
]);

you should try this it may be solve your problem you have to read multiple dimensional array and how to fetch then you can solve your problem .

Upvotes: 1

Related Questions