php - laravel 7 store multiple values in different row and also another tables in different row
Get the solution ↓↓↓**help me? I want to input multiple value and store in database with different row. with some other values inset in anther 2 different tables at same time. im not able to code controller store part i spent 3days help me **
example :
In input form, to "table name"( Purchases )
| product quantity price manufacturer |
| a 1 12$ xyz |
| b 2 1$ x |
| c 10 10$ y
also insert selected fled 'product' 'quantity' 'price' this value to "table name"( bill_products)
|| product quantity price |
| a 1 12$ |
| b 2 1$ |
| c 10 10$
also insert selected fled 'product' 'quantity' this value to "table name"( item_lists)
|| product quantity |
| a 1 |
| b 2 |
| c 10
This is my view (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="{{route('purchasesave')}}" 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>
This is my controller (PurchaseController.php) and I have 3 model (Purchase , Bill_product and Item_list )
public function purchasesave(Request $request)
{
}
this is route part
Route::post('purchasesave', 'PurchaseController@purchasesave')->name('purchasesave');
Answer
Solution:
Since you are passing an array for each attribute, you will be able to iterate over it. Please make sure to properly valdiate your data. Afterwards you can simply get the items of each array at the same index. So for example all items at index 0 because they belong together.
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],
]);
}
}
If you don't use models you can simply replace the statements by usingDB
andinsert()
, to insert the data into your DB.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xml parsing error: no root element found
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.