Javascript laravel多行保存到数据库
我是一个拉威尔初学者。目前我正在学习做一个库存系统。我有两张桌子:goodsreceiveheader和goodsreceivedetail 单击“提交”按钮时,如何允许将多行保存到数据库中。希望有人能帮助我,因为我已经坚持了几个星期了=( 对于goodsreceiveheader表,我有以下字段:Javascript laravel多行保存到数据库,javascript,php,laravel,Javascript,Php,Laravel,我是一个拉威尔初学者。目前我正在学习做一个库存系统。我有两张桌子:goodsreceiveheader和goodsreceivedetail 单击“提交”按钮时,如何允许将多行保存到数据库中。希望有人能帮助我,因为我已经坚持了几个星期了=( 对于goodsreceiveheader表,我有以下字段: id, referencenumber, vendorid(FK), date, createdby. id, goodsreceiveheader_id(FK), itemid(FK), qua
id,
referencenumber,
vendorid(FK),
date,
createdby.
id,
goodsreceiveheader_id(FK),
itemid(FK),
quantity,
costprice.
goodsreceivedetail表中,我有以下字段:
id,
referencenumber,
vendorid(FK),
date,
createdby.
id,
goodsreceiveheader_id(FK),
itemid(FK),
quantity,
costprice.
create.blade.php
@extends('admin.layout')
@section('content')
<fieldset>
<legend>Create New Goods Receive</legend>
@include('layouts.error')
{!! Form::open(['url' => 'goodsreceive/save', 'method'=>'post']) !!}
@include('goodsreceiveheader.partial._goodsreceiveheader_form')
{!! Form::close() !!}
</fieldset>
@endsection
使用insert函数并将数据作为多个数组传递,如 如果模型名为“User”,则下面的代码适用于多个条目 用户::插入(['name'=>'xyz'],['name'=>'abc']);已解决
public function save(GoodsreceiveheaderRequest $request)
{
$head = Goodsreceiveheader::findorNew($request->id);
$head->referencenumber=$request->referencenumber;
$head->vendorid=$request->vendorid;
$head->date=$request->date;
$head->createdby=$request->createdby;
if ($head->save()){
$id = $head->id;
foreach($request->itemid as $key =>$item_id){
$data = array(
'goodsreceiveheader_id'=>$id,
'itemid'=>$request->itemid [$key],
'quantity'=>$request->quantity [$key],
'costprice'=>$request->costprice [$key],
);
Goodsreceivedetail::insert($data);
}
}
Session::flash('message','You have successfully create goods receive.');
return redirect('goodsreceive/goodsreceiveheader_list');
}
发布的代码太多。请尝试阅读文档,同样在您的js
addRow
中,js中混合了刀片指令。这将不起作用。请尝试使用数组将多个产品传递给controller,然后再次使用数组保存多个行。您需要阅读一些教程。感谢您的回复。我的js addRow正在按我所能添加的方式工作一行供用户插入他们的项目。用户可以单击“保存”并将数据保存到数据库。只是它只将一行数据保存到数据库,而不是其他行。那么,我能为它做些什么?有什么教程可以推荐给我吗?提前谢谢