mercredi 7 juillet 2021

How to save all rows into database using Laravel?

How to save all rows into database using Laravel?

if click save button then save all rows:

Form screenshot:

enter image description here

Form code:

<!-- Modal Add Wholesale Price -->
    <div class="container mt-3">
      <!-- The Modal -->
        <!-- <div class="modal fade" id="add-wsp"> -->
            <div class="modal-dialog">
                <div class="modal-content"  style="width:1100px;margin-left:-300px;">
                  
                    <!-- Modal Header -->
                    <div class="modal-header">
                      <h4 class="modal-title">Add Wholesale Price</h4>
                      <button type="button" class="close" data-dismiss="modal">×</button>
                    </div>
                    
                    <!-- Modal body -->
                    <div class="modal-body">
                        <form action="" method="POST" class="shadow-lg p-4 w-f">
                        @csrf
                        <div class="modal-date">
                            <label>Select Date:&emsp;&emsp;</label>
                            <input type="text" name="date" placeholder="Select Price Date" class="getdate"/>
                            <button type="button" name="loadform" class="btn btn-warning load-form">Load Form</button>
    
                        </div>
                      <div class="data" style="display:none;">
                        <table class="table table-bordered" id="table">
                            <thead class="bg-success">
                                <tr>
                                    <th scope="col" class="td-size">CCode</th><th scope="col" class="td-size">CUnits</th><th scope="col" class="td-size">CName</th><th scope="col" class="td-size">Vendor 1</th><th scope="col" class="td-size">Vendor 2</th><th scope="col" class="td-size">Vendor 3</th><th scope="col" class="td-size">Vendor 4</th>
                                </tr>
                            </thead>
                            
                                <tr>
                                    <td><input type="text" name="ccode[]" class="td-size addrow" onclick="addrow();"></td><td><input type="text" name="cunit[]" class="td-size"></td><td><input type="text" name="cname[]" class="td-size"></td><td><input type="text" name="vendor1[]" class="td-size"></td><td><input type="text" name="vendor2[]" class="td-size"></td><td><input type="text" name="vendor3[]" class="td-size"></td><td><input type="text" name="vendor4[]" class="td-size"></td>
                                </tr>
                            
                        </table>
                      </div>
                      <div class="modal-footer">
                      <button type="submit" name="save" class="btn btn-success">Save</button>
                    </div>
                    </form>
    
                    </div>
    
                </div>
            </div>
        </div>
    </div>

Form controller:

// Controller Save Community Vendor data

    function commvendordata(Request $request){
    $request->validate([
        'date'=>'required',
        'ccode[]'=>'required',
        'cunit[]'=>'required',
        'cname[]'=>'required',
        'vendor1[]'=>'required',
        'vendor2[]'=>'required',
        'vendor3[]'=>'required',
        'vendor4[]'=>'required',
    ]);
    // Insert data into database
    $cvendordata = new commvendordata;
    $cvendordata->date = $request->date;
    $cvendordata->cd_id = $request->ccode;
    $cvendordata->unit_id = $request->cunit;
    $cvendordata->cn_id = $request->cname;
    $cvendordata->vender1 = $request->vendor1;
    $cvendordata->vender2 = $request->vendor2;
    $cvendordata->vender3 = $request->vendor3;
    $cvendordata->vender4 = $request->vendor4;
    $save = $cvendordata->save();
    if($save){
        return back()->with('success','Added to database');
    }else{
        return back()->with('fail','Something Wrong');
    }
}

Web route:

// Communitedy vender data

Route::post('/auth/admin/commvendordata',[MainController::class,'commvendordata'])->name('auth.commvendordata');

Database table screenshot:

enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire