【发布时间】:2018-08-06 00:02:04
【问题描述】:
我尝试了我在研究过程中发现的所有解决方案,但似乎没有什么对我有用。这是我正在尝试做的事情的概述。作为管理员,我控制客户的活动。客户是提出请求的人,我作为管理员批准该请求。
桌子
包
编号 |客户 ID | Bearer_id |状态
包装_产品
编号 | package_id |产品ID |数量
刀片
在我的刀片中,我做了一个 foreach 来显示当前在客户购物车上的当前产品。此刀片已在当前选定客户的页面上。
<div class="panel-wrapper collapse in">
<div class="panel-body">
{!!Form::open(array('route'=>'storeProduct', 'id' => 'example-advanced-form', 'method' => 'post'))!!}
<div class="table-wrap">
<div class="table-responsive">
<table id="datable_1" class="table table-hover display pb-30" >
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Qty</th>
<th class="column-spacer"></th>
</tr>
</thead>
<input type="hidden" name="customerid" value="{{$customers->id}}">
@foreach($customers->products as $product)
<tbody>
<tr>
<td><input type="hidden" name="productid[]" value="{{$product->id}}">{{$product->name}}</td>
<td><input type="hidden" name="qty[]" value="{{$product->pivot->qty}}">{{$product->pivot->qty}}</td>
</tr>
</tbody>
@endforeach
</table>
<br>
</div>
@include('errors.list')
<fieldset>
<div class="row">
<center>{!!Form::submit('Process', array('class' => 'btn btn-primary'))!!}</center>
</div>
</fieldset>
</div>
{!!Form::hidden('_token', csrf_token())!!}
{!!Form::close()!!}
</div>
</div>
我使用了 dd(),它显示了产品数组。我可以毫无问题地将请求的数据插入到 packages 表中,但是在将产品数组插入到 package_products 表中时,它只插入最后一行。我需要将这些产品插入不同的行。我缺什么?
控制器
public function storeProduct(Request $request)
{
try{
$package = $this->package;
$productid = $request['productid'];
$qty = $request['qty'];
$bearer = Bearer::status()->package()->first();
$package->customer_id = $request['customerid'];
$package->bearer_id = $bearer->id;
$package->status = 'pending';
$package->save();
if ($package->save()){
$id = $package->id;
$packageproducts = [];
$packageproducts = [
'package_id'=>$id,
'product_id'=>$productid,
'qty'=>$qty];
DB::table('package_products')->insert($packageproducts);
}
return redirect()->route('index')->with('message', 'Package Successfully Added');
}
catch(Exception $e){
return redirect()->back()->withErrors(['Error']);
}
}
【问题讨论】:
-
您正在保存
$package->save()两次 -
你在哪里分配 $packageproducts 数组中的值,你正在插入?
-
@AfshanShujat 抱歉,我仍在重命名我的表格。我编辑了它 $packageproducts。 Akintunde007 哦,我不知道没有那个我还能保存它,谢谢,但它仍然插入最后一行。