Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nanadjei2's avatar

Laravel 5.4 Dynamic form save data to database

I am building a Point of Sale system for a pharmaceutical company. I am allowing the users to add more fields when a new stock is purchased and they want to add them to the existing ones. But I want to submit all of them at once no matter the number of fields they add. I am finding it difficult to do that in the controller. I get this error anytime I try submitting the form Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in

This is my HTML

 <div class="container">
     <div class="">
     <div class="row">
       <div class="col-md-8 col-md-offset-2 shad-content">
         <div class="panel-heading "><h3>Please add new drugs</h3></div>
             <div class="panel-body">
              <form action="{{ route('post-new-products') }}" method="POST">
                 {{ csrf_field() }}
                 <table id="add-me" class="table table-bordered">
                <thead>
                  <tr>
                   <th>Quantity</th>
                   <th>Description</th>
                   <th>Selling Price</th>
                   <th>Actions</th>
                 </tr>
              </thead>
              <tbody >
                <tr>
                <td id="quantity" class="col-md-2"><input onkeypress='return event.charCode >= 48 && event.charCode <=57' type="text" name="quantity[]" class="form-control" autofocus="" /></td>
                <td class="col-md-7"><input type="text" name="description[]" class="form-control" /></td>
                <td class="col-md-3"><input type="text" name="selling_price[]" class="form-control" /></td>
                <td class="col-md-2">
                    <button type="button" class="btn btn-danger">
                    Delete</button>
                </td>
              </tr>
            </tbody>
        </table>
        <div class="action-buttons">
            <button id="add-form" type="button" class="btn btn-default">Add New Form</button>
            <button type="submit" class="btn btn-success">Save All Drugs</button>
        </div>
        </form>
      </div>
    </div>
  </div>    
</div>

This is jQuery to add form dynamically

  $('#add-form').click(function() {
       i++;
        $('#add-me').append(
           '<tbody id="row'+i+'"><tr>'+
             '<td class="col-md-2">'+
                '<input id="quantity" onkeypress="return event.charCode >= 48 && event.charCode <=57" type="text" name="quantity[]" class="form-control"/>'
            +'</td>'
            +'<td class="col-md-7">'
                +'<input type="text" name="description[]" class="form-control"/>'
            +'</td>'
            +'<td class="col-md-3">'
                +'<input type="text" name="selling_price[]" class="form-control" />'
            +'</td>'
            +'<td class="col-md-2">'
                +'<button id="'+i+'" type="button" class="btn btn-danger delegated-btn">Delete</button>'
            +'</td>'
        +'</tr></tbody>'
        );
  });

This is my CONTROLLER

  public function createInventory(Request $request) {
      $product = new Product;
      $product->quantities = $request->get('quantity');
      $product->description = $request->get('description');
      $product->selling_price = $request->get('selling_price');
  $product->save();
} 
0 likes
31 replies
bipin's avatar

this error occur because you don't have dynamic column(increasing column ,mean you have to create column every time manually in Db) in you DB so use big data for this kind of thing but still if you need to perform these thing in other data for that you need to store data in row wise rather than column wise hope this help

1 like
nanadjei2's avatar

How can i do it using big data ? Kindly show me some example

bipin's avatar

use mongo db, you will find example on google if you are using other database then create new table where you store all your row wise increase data(dynamic form data) and pass there id into other table(2 one)

nanadjei2's avatar

Is there no other way i can do it with laravel and mysql? If i don't want to use mongo db, what should i do to make this work?

nanadjei2's avatar

@shakti I have checked all those links before I posted this. But none of those seem not to help. But I will still go through again and see.

bipin's avatar

@nanadjei2 , yes you can create dynamic form and save it into mysql . 1.you need to create 2 (menu and submenu) table 2.just save all dynamic form data in one table(eg supoose it name is menu) in row wise manner 3.pass its id to 2 table(eg supoose it name is submenu)

your script look fine so don't need to modify just check is its getting array of value in your controller

then in your controller use forloop to save it into database for eg

       foreach ($id as $lp) {
          $fnew=$lp;
        }

then use any loop to save it on database i m using while loop to save one by one array data into database for eg suppose i have 3 array:-1 array containt "jhon" 2 array containt "bipin" 3 array containt "sam" here while loop save 1 array value on database and it continue to save value into databse untill while loop break

       $j=0;
       while($j < count($request->get('name'))) {
       $Val             = $fnew[$j];
                
        write  insert it into DB
    
  
1 like
bipin's avatar

@nanadjei2 error which you getting is because of your controller since your passing array of data and try to save it directly into database just use above code to save it on database it will work perfectly hope this will help!!!!

nanadjei2's avatar

Thank you guys for our suggestions but am still facing some few errors

@bipin I have restructured my html and controller when I wasn't getting a response I tried to look at some examples but am having a new error. This is my html

       <tbody >
                  <tr>
                    <td id="quantity" class="col-md-2"><input onkeypress='return event.charCode >= 48 && event.charCode <=57' type="text" name="rows[0]quantity[]" class="form-control" autofocus="" /></td>
                    <td class="col-md-7"><input type="text" name="rows[0]description[]" class="form-control" /></td>
                    <td class="col-md-3"><input type="text" name="rows[0]selling_price[]" class="form-control" /></td>
                    <td class="col-md-2">
                        <button type="button" class="btn btn-danger">
                        Delete</button>
                    </td>
                  </tr>
              </tbody>

This is my jquery

   '<tbody id="row'+i+'"><tr>'+
                '<td class="col-md-2">'+
                    '<input id="quantity" onkeypress="return event.charCode >= 48 && event.charCode <=57" type="text" name="rows['+i+']quantity[]" class="form-control"/>'
                +'</td>'
                +'<td class="col-md-7">'
                    +'<input type="text" name="rows['+i+']description[]" class="form-control"/>'
                +'</td>'
                +'<td class="col-md-3">'
                    +'<input type="text" name="rows['+i+']selling_price[]" class="form-control" />'
                +'</td>'
                +'<td class="col-md-2">'
                    +'<button id="'+i+'" type="button" class="btn btn-danger delegated-btn">Delete</button>'
                +'</td>'

            +'</tr></tbody>'

I am incrementing i

   This is my new controller

    public function createInventory(Request $request) {
    $input = $request->all();
        foreach ($input['rows'] as $row) {
            $product = new Product([
                'quantity' => $row['quantity'],
                'description' => $row['description'],
                'selling_price' => $row['selling_price']
            ]);
            dd($product);
        }
} 

But I am getting this error (1/1) ErrorException Illegal string offset 'quantity'

shakti's avatar

have you done some dd for your input field

if possible do dd($input) and show us the result which you get

nanadjei2's avatar

@shakti THis is the what i get after dd($input)

array:2 [▼ "_token" => "J9R8kJKdJj1am6gj8edPtNcfUSyCqPYXiTCKLfDK" "rows" => array:2 [▼ 0 => "55.00" 2 => "4.50"

It looks like it is only getting only the selling_price values

nanadjei2's avatar

@shaki but if i take row[0] out from both my html and jquery, I get this results after dd($input)

array:4 [▼ "_token" => "J9R8kJKdJj1am6gj8edPtNcfUSyCqPYXiTCKLfDK" "quantity" => array:2 [▼ 0 => "55" 1 => "45" ] "description" => array:2 [▼ 0 => "Description" 1 => "Another Description" ] "selling_price" => array:2 [▼ 0 => "5.50" 1 => "4

shakti's avatar

I still thing this is not the correct way to which you have mention in your route

    <tbody >
                  <tr>
                    <td id="quantity" class="col-md-2"><input onkeypress='return event.charCode >= 48 && event.charCode <=57' type="text" name="quantity[]" class="form-control" autofocus="" /></td>
                    <td class="col-md-7"><input type="text" name="description[]" class="form-control" /></td>
                    <td class="col-md-3"><input type="text" name="selling_price[]" class="form-control" /></td>
                    <td class="col-md-2">
                        <button type="button" class="btn btn-danger">
                        Delete</button>
                    </td>
                  </tr>
              </tbody>

do same with your jquery

 '<tbody id="row'+i+'"><tr>'+
                '<td class="col-md-2">'+
                    '<input id="quantity" onkeypress="return event.charCode >= 48 && event.charCode <=57" type="text" name="quantity[]" class="form-control"/>'
                +'</td>'
                +'<td class="col-md-7">'
                    +'<input type="text" name="description[]" class="form-control"/>'
                +'</td>'
                +'<td class="col-md-3">'
                    +'<input type="text" name="selling_price[]" class="form-control" />'
                +'</td>'
                +'<td class="col-md-2">'
                    +'<button id="'+i+'" type="button" class="btn btn-danger delegated-btn">Delete</button>'
                +'</td>'

            +'</tr></tbody>'

In controller

public function createInventory(Request $request) {
    $input = $request->all();
        
            dd($input);
        }
} 
nanadjei2's avatar

@shakti So you mean I should go back to the old way which I included the row[] ? Like so

    <tbody >
                  <tr>
                    <td id="quantity" class="col-md-2"><input onkeypress='return event.charCode >= 48 && event.charCode <=57' type="text" name="rows[0]quantity[]" class="form-control" autofocus="" /></td>
                    <td class="col-md-7"><input type="text" name="rows[0]description[]" class="form-control" /></td>
                    <td class="col-md-3"><input type="text" name="rows[0]selling_price[]" class="form-control" /></td>
                    <td class="col-md-2">
                        <button type="button" class="btn btn-danger">
                        Delete</button>
                    </td>
                  </tr>
              </tbody>
shakti's avatar

yes ..try to do the above code and show me what you get from the dd ...

may be after that i can help you

nanadjei2's avatar

@jha I am getting this array:2 [▼ "_token" => "J9R8kJKdJj1am6gj8edPtNcfUSyCqPYXiTCKLfDK" "rows" => array:2 [▼ 0 => "55.00" 1 => "45.00"

if row[0] ++

bipin's avatar

@nanadjei2 this error because of row

use this thing in view

           <input type="text"   name="description[]" id="des">
             <input type="text"   name="selling[]" id="sell">
                <input type="text"   name="quantity[]" id="quan">

and apply your own jquery in your controller your controller

       foreach($requst->get('des') as $values){
           $f[]   = $values;
          }
         foreach($requst->get('sell') as $values){
           $k[]   = $values1;
          }
          foreach($requst->get('quan') as $values){
           $t[]   = $values2;
          }
        $j = 0;
         while($j < count($request->get('des'))) {

           $Val            = $f[$j];
            $new        = $k[$j];
            $new1  =$t[$j];
             DB::insert('insert into 'tablename' 
      (coulnmname1,coulnmname2,coulnmname3) values(?,?,?)',
    [$Val,$new,$new1);

hope this will solve your issue

1 like
nanadjei2's avatar

The browser keeps loading and nothing happends

nanadjei2's avatar

This is my controller. Please check if i did anything wrong and help me cos i really wanna fix this.

public function createInventory(Request $request) {

    foreach($request->get('quantity') as $value) {
        $f[] = $value;
    }
    foreach($request->get('description') as $value) {
        $k[] = $value;
    }
    foreach($request->get('selling_price') as $value) {
        $t[] = $value;
    }
    
    $j = 0;
    while ($j < count($request->get('description'))) {
        $Val = $f[$j];
        $new = $k[$j];
        $new1 = $t[$j];

        DB::insert('insert into `products`
            (quantity, description, selling_price) values(?,?,?)', [$Val,$new,$new1]);
       }

     }
shakti's avatar

for me do something like this

public function createInventory(Request $request) {

   $quantity=$request->get('quantity');
  $description=$request->get('description'); 
  $sell=$request->get('selling_price');  
foreach($quantity as $key=>$val){

        DB::table('product')->insert(
            ['quantity'=>$val, 
        'description'=>$description[$key], 
        'selling_price'=>$sell[$key]
        ]
    ) ;
    }
}
3 likes
bipin's avatar

@nanadjei2 i think this error because of foreach loop look at your foreach loop your are using same variable in 3 of them for eg

   foreach($request->get('quantity') as $value) {
      $f[] = $value;
     }
     foreach($request->get('description') as $value) {
        $k[] = $value;
         }
        foreach($request->get('selling_price') as $value) {
           $t[] = $value;
      }

modify your code like describe in below

      foreach($request->get('quantity') as $value) {
         $f[] = $value;
        }
     foreach($request->get('description') as $va) {
        $k[] = $va;
         }
        foreach($request->get('selling_price') as $value111) {
           $t[] = $value111;
      } 

    
nanadjei2's avatar

@jha Your worked but i was sending two data. only the 1st form was sent and when i look into the database, i have 25 records of the same 1st form that was sent.

shakti's avatar

@nanadjei2 sorry but didnt get you can you show me your code so that it can be easy for me to understand

nanadjei2's avatar

@bipin am getting this error SQLSTATE[22007]: Invalid datetime format: 1978 Incorrect default value '0000-00-00 00:00:00' for column 'updated_at' (SQL: insert intoproducts(quantity, description, selling_price) values(55,Description,5.50))

In the 1st place i think it is even posting only the 1st form. is my html and jquery correct?

    <tbody >
                  <tr>
                    <td id="quantity" class="col-md-2"><input onkeypress='return event.charCode >= 48 && event.charCode <=57' type="text" name="quantity[]" id="quan" class="form-control" autofocus="" /></td>
                    <td class="col-md-7"><input type="text" name="description[]" id="des" class="form-control" /></td>
                    <td class="col-md-3"><input type="text" name="selling_price[]" id="sell" class="form-control" /></td>
                    <td class="col-md-2">
                        <button type="button" class="btn btn-danger">
                        Delete</button>
                    </td>
                  </tr>
          </tbody>

My Jquery:

      '<tbody id="row'+i+'"><tr>'+
                '<td class="col-md-2">'+
                    '<input id="quantity" onkeypress="return event.charCode >= 48 && event.charCode <=57" type="text" name="quantity[]" id="quan" class="form-control"/>'
                +'</td>'
                +'<td class="col-md-7">'
                    +'<input type="text" name="description[]" id="des" class="form-control"/>'
                +'</td>'
                +'<td class="col-md-3">'
                    +'<input type="text" name="selling_price[]" id="sell" class="form-control" />'
                +'</td>'
                +'<td class="col-md-2">'
                    +'<button id="'+i+'" type="button" class="btn btn-danger delegated-btn">Delete</button>'
                +'</td>'
         +'</tr></tbody>'
nanadjei2's avatar

@jha This is the controller...

   $quantity=$request->get('quantity');
   $description=$request->get('description'); 
   $sell=$request->get('selling_price');  
      foreach($quantity as $key=>$val){
         DB::table('products')->insert(
        [
            'quantity'=>$val, 
            'description'=>$description[$key], 
            'selling_price'=>$sell[$key]
        ]
    ) ;

Please kindly write the codes well for me.

bipin's avatar

@nanadjei2 this error occur because you are trying to store invalide date formate in your table or your created_at ,update_at column is not nullable

1 like
nanadjei2's avatar

Please guys kindly stick around and help me to get this done. Am pleading...

extjac's avatar
extjac
Best Answer
Level 6
for ($i=0; $i < count($input['quantity']); ++$i) 
{
    $products= new products;        
    $products->quantity = $input['quantity'][$i];
    $products->description= $input['description'][$i];
    $products->selling_price= $input['selling_price'][$i];
    $products->save();  
}
3 likes
nanadjei2's avatar

@extjac Oh my God...!! I cant believe It worked. I don't even know what to say. All the data are just posting in the right order. Am so amazed...!!!

Next

Please or to participate in this conversation.