首页 > 解决方案 > 如何在 Laravel 中将数据从 html 表保存到数据库

问题描述

我通过使用将文本字段中的数据输入到 html 表格行JavaScript。然后我需要将它们mysql一一保存在一个表中。所以我想知道如何将它们保存到数据库中。我把代码放在下面。

function addRow() {
  var table = document.getElementById("tbody");
  var rowCount = table.rows.length;
  var row = table.insertRow(rowCount);

  var items = document.getElementById("item").value;
  var suppliers = document.getElementById("supplier").value;
  var quantities = document.getElementById("quantity").value;
  var grnprices = document.getElementById("grnprice").value;



  if (items == "", suppliers == "", quantities == "", grnprices == "") {
    alert("Please fill the Required Field");
  } else {

    var values = parseInt(document.getElementById('rawno').value, 10);
    values = isNaN(values) ? 0 : values;
    values++;
    document.getElementById('rawno').value = values;

    var total = quantities * grnprices;

    row.insertCell(0).innerHTML = values;
    row.insertCell(1).innerHTML = items;
    row.insertCell(2).innerHTML = suppliers;
    row.insertCell(3).innerHTML = quantities;
    row.insertCell(4).innerHTML = "Rs. " + grnprices;
    row.insertCell(5).innerHTML = "Rs. " + total;
    row.insertCell(6).innerHTML = '<button type ="button" class="btn btn-danger" onClick="Javacsript:deleteRow(this)"> <i class="fa fa-trash-o"></i></button>';
  }
}
<table class="table table-striped">
  <thead>
    <tr>
      <th scope="col" width="200">Item</th>
      <th scope="col" width="200">Supplier</th>
      <th scope="col" width="200">Quantity</th>
      <th scope="col" width="200">GRN Price</th>
      <th scope="col" width="50"></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input type="text" min="0" class="form-control" name="item" id="item">
      </td>
      <td>
        <input type="number" min="0" class="form-control" name="supplier" id="supplier">
      </td>
      <td>
        <input type="number" min="1" class="form-control" name="quantity" id="quantity">
      </td>
      <td>
        <input type="number" min="0" class="form-control" name="grnprice" id="grnprice">
      </td>
      <td>
        <input type="button" class="btn btn-info" id="add" value="Add" onclick="Javascript:addRow()">
      </td>
      <td>
        <input type="hidden" name="rawno" id="rawno">
      </td>
    </tr>
  </tbody>
</table>
</div>



<table class="table" id="myTableData">
  <thead>
    <tr>
      <th>No</th>
      <th>Item</th>
      <th>Supplier</th>
      <th>Quantity</th>
      <th>GRN Price</th>
      <th>Total</th>
      <th></th>
    </tr>
  </thead>

  <tbody id="tbody">
    <tr></tr>
  </tbody>
</table>

<button type="button" class="btn btn-success" onclick="grnConfirmation()">Save</button>

谁能举个例子。

标签: javascriptphplaravel

解决方案


首先,您需要将所有要保存在数据库中的输入数据分组到一个指向控制器方法的表单中。然后控制器将完成所有工作。

假设您在刀片文件中编写前端:create.blade.php

<form method="POST" action="ItemInfoController@store">

<input type="text" min="0" class="form-control" name="item" id="item">
<input type="number" min="0" class="form-control" name="supplier" id="supplier">
<input type="number" min="1" class="form-control" name="quantity" id="quantity">
<input type="number" min="0" class="form-control" name="grnprice" id="grnprice">
<input type="submit" value="submit">

</form>

在 ItemInfoController.php 中:

public function store(Request $request) {

// This will add the data from input to the database.
// You can change the query builder as you need.
DB::table('item')->insert(['item' => $request->item, 
                           'supplier' => $request->supplier, 
                           'quantity' => $request->quantity]);

}

如果您想使用 Eloquent,您可以使用DB您使用的模型更改查询。

例子 :ItemInfo::create($request);


推荐阅读