![](/img/trans.png)
[英]How can I use multple textbox with different names in array to to insert multiple rows in mysql
[英]How to insert an array into different rows?
我嘗試了研究期間發現的所有解決方案,但似乎沒有任何效果對我有用。 這是我正在嘗試做的概述。 作為管理員,我控制客戶的活動。 客戶是發出請求的人,我作為管理員批准了該請求。
桌子
配套
id | customer_id | bearer_id | 狀態
包裝產品
id | package_id | product_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(),它顯示了產品數組。 我可以毫無問題地將請求的數據插入到包表中,但是將產品數組插入到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 = App\Models\Package::first();
$package->products()->attach($packageProducts);
我終於找到了解決方案,我所缺少的是在函數中放入for循環,以便它獲取所需的數組並將其保存到數據庫的不同行中。 這是我的答案
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';
if ($package->save()){
$id = $package->id;
$packageproducts = [];
for(i=0; < count($productid); $i++){
$packageproducts[] = [
'package_id'=>$id,
'product_id'=>$productid[$i],
'qty'=>$qty[$i],];
}
DB::table('package_products')->insert($packageproducts);
}
return redirect()->route('index')->with('message', 'Package Successfully Added');
}
catch(Exception $e){
return redirect()->back()->withErrors(['Error']);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.