나는 초보자 다. 현재 재고 시스템을 배우고 있습니다. 나는 두 개의 테이블을 가지고있다 : 물품 수신 헤더와 물품 수신 된 데이터.laravel mutliple row 데이터베이스에 저장
제출 버튼을 클릭 할 때 데이터베이스에 여러 행을 저장할 수 있도록하려면 어떻게해야합니까? 나는 몇 주 = (goodsreceiveheader 테이블의 경우
이 붙어했던대로 누군가가 나를 도울 것입니다 희망, 내가의 필드가 :
id,
referencenumber,
vendorid(FK),
date,
createdby.
goodsreceivedetail 테이블 동안, 난의 필드가 :
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
내보기 :
<style>
div#gr{
padding: 20px;
border:1px solid black;
}
</style>
<div class="container">
<h2>Goods Receive</h2>
<hr>
<div id="gr" class="row" style="background-color: lightgoldenrodyellow">
<div class="col-lg-4 col-sm-6">
<input name="createdby" type="hidden" value="{{ Auth::user()->name }}">
{!! Form::label('referencenumber', 'Reference Number:')!!}
<div class="form-group">
<input type="text" name="referencenumber" class="form-control" placeholder="Reference Number">
</div>
</div>
<div class="col-lg-4 col-sm-6">
{!! Form::label('date', 'Receive Date:')!!}
{!! Form::date('date',null,['class'=>'form-control']) !!}
</div>
<div class="col-lg-4 col-sm-6">
{!! Form::label('vendorid', 'Vendor ID:')!!}
<select name="vendorid" class="form-control">
<option value="" selected disabled>Please Select Vendor..</option>
@foreach($vendors as $vendor)
<option value="{{$vendor->id}}">{{$vendor->vendorid}}</option>
@endforeach
</select>
</div>
</div>
<br>
<table class="table table-bordered">
<thead>
<th>Item Barcode</th>
<th>Quantity</th>
<th>Cost Price</th>
<th style="text-align: center;background: #eee">
<a href="#" onclick="addRow()">
<i class="glyphicon glyphicon-plus"></i>
</a>
</th>
</thead>
<tbody>
<tr>
<td>
<select class="form-control" name="itemid">
<option value="" selected disabled>Select Barcode</option>
@foreach($items as $item)
<option value="{{$item->itemid}}">{{$item->itembarcode}}</option>
@endforeach
</select>
</td>
<td><input type="text" name="quantity" class="form-control quantity"></td>
<td><input type="text" name="costprice" class="form-control costprice"></td>
<td style="text-align: center" onclick="cannotdelete()">
<a href="#" class="btn btn-danger remove">
<i class="fa fa-times"></i>
</a>
</td>
</tr>
</tbody>
</table>
<br>
<button type="submit" class="btn btn-primary pull-right">Submit</button>
</div>
<script type="text/javascript">
function addRow()
{
var tr='<tr>'+
'<td>'+
'<select class="form-control" name="itemid">'+
'<option value="" selected disabled>Select Barcode</option>'+
'@foreach($items as $item)'+
'<option value="{{$item->itemid}}">{{$item->itembarcode}}</option>'+
'@endforeach'+
'</select>'+
'</td>'+
'<td><input type="text" name="quantity" class="form-control quantity"></td>'+
'<td><input type="text" name="costprice" class="form-control costprice"></td>'+
'<td class="remove" style="text-align: center"><a href="#" class="btn btn-danger" onclick="deleteRow()"><i class="fa fa-times"></i></a></td>'+
'</tr>';
$('tbody').append(tr);
}
function deleteRow()
{
$(document).on('click', '.remove', function()
{
$(this).parent('tr').remove();
});
}
function cannotdelete()
{
alert('You cannot delete the first row!!!')
}
</script>
내 컨트롤러 :
public function save(GoodsreceiveheaderRequest $request)
{ $data = array(
'referencenumber'=>$request->referencenumber,
'vendorid'=>$request->vendorid,
'date'=>$request->date,
'createdby'=>$request->createdby,
);
$i = DB::table('goodsreceiveheader')->insertGetId($data);
$goodsreceivedetail = array(
'goodsreceiveheader_id'=>$i,
'itemid'=>$request->itemid,
'quantity'=>$request->quantity,
'costprice'=>$request->costprice,
);
$s = DB::table('goodsreceivedetail')->insert($goodsreceivedetail);
Session::flash('message','You have successfully create goods receive.');
return redirect('goodsreceive/goodsreceiveheader_list');
}
너무 많은 코드가 게시되었습니다. js의'addRow'에도 js에 혼합 된 블레이드 지시문이 있습니다. 설명서가 제대로 작동하지 않습니다. 여러 제품을 컨트롤러에 전달한 다음 배열을 다시 전달하여 배열을 여러 개 저장하는 작업을 시도해보십시오. 자습서를 읽어야합니다. – Gntem
답장을 보내 주셔서 감사합니다. js addRow는 사용자 항목을 삽입 할 행을 추가 할 수 있으므로 작동합니다. 사용자는 저장을 클릭하여 데이터를 데이터베이스에 저장할 수 있습니다. 단지 그것의 대신 하나의 데이터 행을 db에 저장하는 것입니다. 그래서, 나는 그것을 위해 무엇을 할 수 있습니까? 저에게 추천할만한 튜토리얼이 있습니까? 미리 감사드립니다. – Nick