Home > Mobile >  laravel submitting multiple rows of a form to mysql by creating duplicate rows
laravel submitting multiple rows of a form to mysql by creating duplicate rows

Time:09-08

i have this form in larvel that will submit data to mysql database every time i click Add Morenew row is created using script i want to submit all the rows at ones

<form action={{route('invetory.create')}} method="post">
    @csrf
    <div id="service">
        <input type="text" name="item"/>
        <input type="text" name="tax" />
        <input name="price" type="text" />
        <input name="selling_price" type="text" />
        <input name="total_price" type="text" />
        <input type="submit"/>
    </div>
</form>   
<a  id="addmore"   onclick="duplicate()">Add More</a>

input fields duplicating script

<script>
function duplicate() {    
    var original = document.getElementById('service');
    var rows = original.parentNode.rows;
    var i = rows.length - 1;
    var clone = original.cloneNode(true); // "deep" clone
    clone.id = "duplic"   (i); // there can only be one element with an ID
    original.parentNode.insertBefore(clone, rows[i]);
}</script>

invetory.create route in web.php

`Route::post('/inventory', 'App\Http\Controllers\InventoryController@create')->name('invetory.create');`

funtion in InventoryController.php

public function create(Request $REQUEST){
    Inventory::Create($REQUEST->all());
    return redirect()->action([InventoryController::class, 'index']);
}

CodePudding user response:

If I am not wrong, what I get from your question is that you want to insert multiple data in single query.

You have to give field name using array other wise it is difficult to differentiate multiple data using same key name. Array will differentiate your data using index value with same name.

Here is a simple example what you want to implement. According to this you can create your own format. Here I used table structure, if you want you can use div structure.

In Blade file:-

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
   
<div > 
   
    <form action="{{route('invetory.create')}}" method="POST">
   
        <table  id="dynamicTable">  
            <tr>
                <th>item</th>
                <th>tax</th>
                <th>price</th>
                <th>selling_price</th>
                <th>total_price</th>
                <th>Action</th>
            </tr>
            <tr>  
                <td><input type="text" name="invetory[0][item]"   /></td>  
                <td><input type="text" name="invetory[0][tax]"   /></td>  
                <td><input type="text" name="invetory[0][price]"   /></td>
                 <td><input type="text" name="invetory[0][selling_price]"   /></td>
                  <td><input type="text" name="invetory[0][total_price]"   /></td>

            </tr>  
        </table> 
       <button type="button" name="add" id="add" >Add More</button>
    
        <button type="submit" >Save</button>
    </form>
</div>
   
<script type="text/javascript">
   
    var i = 0;
       
    $("#add").click(function(){
   
          i;
   
        $("#dynamicTable").append('<tr><td><input type="text" name="invetory[' i '][item]"   /></td><td><input type="text" name="invetory[' i '][tax]"   /></td><td><input type="text" name="invetory[' i '][price]"  /></td><td><input type="text" name="invetory[' i '][selling_price]"  /></td><td><input type="text" name="invetory[' i '][total_price]"  /></td><td><button type="button" >Remove</button></td></tr>');
    });
   
    $(document).on('click', '.remove-tr', function(){  
         $(this).parents('tr').remove();
    });  
   
</script>
  
</body>
</html>

And in controller you have to do like this

public function create(Request $request){
    foreach ($request->invetory as $key => $value) {
        Inventory::create($value);
    }
    return redirect()->action([InventoryController::class, 'index']);
}

CodePudding user response:

You may use

public function create(Request $REQUEST){
Inventory::firstOrCreate($REQUEST->all());
return redirect()->action([InventoryController::class, 'index']);

}

You may also check laravel docs

  • Related