I'm trying to pass multiple arrays via a form to a Laravel Controller and attach form values to a relationship.
I've tried to loop through the Request array but i get an 'undefined index' error and i know i'm not getting to the data somehow. Thanks in advance.
form.php
<form method="POST" action="{{ route('/trs') }}">
@csrf
<input type="hidden" name="name[]" value="{{ $item->name }}" />
<input type="hidden" name="product_id[]" value="{{ $item->product_id }}" />
<input type="hidden" name="price[]" value="{{ $item->price }}" />
<input type="hidden" name="quantity[]" value="{{ $item->quantity }}" />
<button type="submit" class="btn btn-primary">
Buy
</button>
</form
Controller
public function purchase(Request $request)
{
$data = $request->except('_token');
$user= User::find(Auth::user()->id);
foreach ($data as $item) {
$ids = $item['product_id'];
$names = $item['name'];
$prices = $item['price'];
$quantities = $item['quantity'];
$orders->products()->attach([$ids => ['quantity' => $quantities, 'price' => $prices]]);
}
}
product_idbeing submitted through the request? i don't see a hidden input for that in form.php. trying to access$item['product_id']whenproduct_idhasn't been set might explain why you are getting that error.