我是Laravel的新手,我正在尝试根据此tutorial向多个表中插入多数据。我复制了项目,但是当我插入表单时,数据无法传递到数据库。通过PhPMyAdmin手动插入时可以看到数据,因此我怀疑这是数据库连接问题。
表单视图:
<form>
<section>
<div class="panel panel-header">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" name="customer_name" class="form-control" placeholder="Please enter your name">
</div></div>
<div class="col-md-6">
<div class="form-group">
<input type="text" name="customer_address" class="form-control" placeholder="Please enter your Address">
</div></div>
</div></div>
<div class="panel panel-footer" >
<table class="table table-bordered">
<thead>
<tr>
<th>Product Name</th>
<th>Brand</th>
<th>Quantity</th>
<th>Budget</th>
<th>Amount</th>
<th><a href="#" class="addRow"><i class="glyphicon glyphicon-plus"></i></a></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="product_name[]" class="form-control" required=""></td>
<td><input type="text" name="brand[]" class="form-control"></td>
<td><input type="text" name="quantity[]" class="form-control quantity" required=""></td>
<td><input type="text" name="budget[]" class="form-control budget"></td>
<td><input type="text" name="amount[]" class="form-control amount"></td>
<td><a href="#" class="btn btn-danger remove"><i class="glyphicon glyphicon-remove"></i></a></td>
</tr>
</tbody>
<tfoot>
<tr>
<td><input type="submit" name="" value="Submit" class="btn btn-success"></td>
</tr>
</tfoot>
</table>
</div>
</section>
</form>
<script type="text/javascript">
$('.addRow').on('click',function(){
addRow();
});
function addRow()
{
var tr='<tr>'+
'<td><input type="text" name="product_name[]" class="form-control" required=""></td>'+
'<td><input type="text" name="brand[]" class="form-control"></td>'+
'<td><input type="text" name="quantity[]" class="form-control quantity" required=""></td>'+
'<td><input type="text" name="budget[]" class="form-control budget"></td>'+
' <td><input type="text" name="amount[]" class="form-control amount"></td>'+
'<td><a href="#" class="btn btn-danger remove"><i class="glyphicon glyphicon-remove"></i></a></td>'+
'</tr>';
$('tbody').append(tr);
};
$('.remove').live('click',function(){
var last=$('tbody tr').length;
if(last==1){
alert("you can not remove last row");
}
else{
$(this).parent().parent().remove();
}
});
</script>
OrderController存储函数:
public function store(Request $request)
{
$data=$request->all();
$lastid=Orders::create($data)->id;
if(count($request->product_name) > 0) {
foreach($request->product_name as $item=>$v) {
$data2=array(
'orders_id'=>$lastid,
'product_name'=>$request->product_name[$item],
'brand'=>$request->brand[$item],
'quantity'=>$request->quantity[$item],
'budget'=>$request->budget[$item],
'amount'=>$request->amount[$item]
);
Items::insert($data2);
}
}
return redirect()->back()->with('success','data insert successfully');
}
路线为:
Route::post('/orders','OrderController@store');
Route::get('/orders','OrderController@index');
Route::get('/items/{id}','OrderController@items');
我在做什么错?还是仅仅是兼容性问题? 谢谢
答案 0 :(得分:1)
您忘记将操作和方法添加到<form>
标记中。我认为这就是表单不发送数据的原因。
另外,您还需要在请求中发送csrf令牌。
<form method="POST" action="/orders">
@csrf
// ...
</form>