I submitting the arrays value from the html and recieving them into the Laravel controller, the problem is I have multiple arrays with seprate attributes arrays. I want to store the info into the database as a collection. Let me explain with example
HTML CODE
<input type="text" name="tw_name[]" placeholder="name" value="{{ old('tw_name') }}">
<input type="text" name="tw_price[]" placeholder="price" value="{{ old('tw_price') }}">
Now I am receving the tw_name and tw_prices arrays in the controller, but I want to store them into the database using model like below
Bet::create([
'name' => 'first_name',
'price' => '10',
]);
Bet::create([
'name' => 'second_name',
'price' => '20',
]);
User can add 12 forms, so we can’t say that user can submit 3 form or 5 form or 10. So we need to handle the same form and save them into database.
if the user will submit 3 form it we will recive 3 arrays in controller
Bet::create([
'name' => 'first_name',
'price' => '10',
]);
Bet::create([
'name' => 'second_name',
'price' => '20',
]);
Bet::create([
'name' => 'thir_name',
'price' => '30',
]);
How can I save them into database, Thank you
>Solution :
A single for() or foreach() loop can be used to save these to the database:
for method:
for($i = 0; $i < count($request->input('tw_name')); $i++) {
Bet::create([
'name' => $request->input('tw_name')[$i],
'price' => $request->input('tw_price')[$i]
]);
}
foreach method:
foreach($request->input('tw_name') as $index => $twName) {
Bet::create([
'name' => $twName,
'price' => $request->input('tw_price')[$index]
]);
}
As long as both tw_name[] and tw_price[] arrays are the same length, this code will create a Bet instance for each pair of uploaded values.