I want to store multiple rows in multiple tables within my database. My controller is currently not storing multiple values, it is only saving one row in my bill_products
and item_lists
tables.
purchases
table:
| product quantity price manufacturer |
| a 1 12$ xyz |
| b 2 1$ x |
| c 10 10$ y |
bill_products
table:
| product quantity price |
| a 1 12$ |
| b 2 1$ |
| c 10 10$ |
item_lists
table:
| product quantity |
| a 1 |
| b 2 |
| c 10 |
This is my purchase\purchase-entry.blade.php
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">PURCHASE FORM</div>
<div class="card-body">
@if (Session::has('success'))
<div class="alert alert-success">{!! Session::get('success') !!}</div>
@endif
@if (Session::has('failure'))
<div class="alert alert-danger">{!! Session::get('failure') !!}</div>
@endif
<form method="POST" action="" enctype="multipart/form-data">
@csrf
<div class="form-row">
<div class="col-md-4 mb-3">
<label >product NAME</label>
<input type="text" name="product[]" placeholder="product" value="" required >
</div>
<div class="col-md-4 mb-3">
<label >quantity</label>
<input type="text" name="quantity[]" placeholder="quantity" value="" required >
</div>
<div class="col-md-4 mb-3">
<label >price</label>
<input type="text" name="price[]" placeholder=price" value="" required >
</div>
<div class="col-md-4 mb-3">
<label >manufacturer</label>
<input type="text" name="manufacturer[]" placeholder="manufacturer" value="" required >
</div>
</div>
</div>
<button class="btn btn-primary float-right" type="submit">SAVE</button>
</form>
The associated route:
Route::post('purchasesave', 'PurchaseController@purchasesave')->name('purchasesave');
My controller PurchaseController.php
uses these 3 models: Purchase
, BillProduct
and ItemList
.
public function purchasesave(Request $request)
{
//insert your validation here
foreach($request->product as $k => $p){
Pruchases::create([
'product' => $request['product'][$k],
'quantity' => $request['quantity'][$k],
'price' => $request['price'][$k],
'manufacturer' => $request['manufacturer'][$k],
]);
BillProduct::create([
'product' => $request['product'][$k],
'quantity' => $request['quantity'][$k],
'price' => $request['price'][$k],
]);
ItemList::create([
'product' => $request['product'][$k],
'quantity' => $request['quantity'][$k],
]);
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire