This form contains three items. I need the customer to be able to tick the required quantity (mnozstvi) for each item. The problem is that I only ever store the single/last item for which the quantity (mnozstvi) has been ticked in the order_items table. At the same time, I need to add a function that if a person does not check anything for one of the items, that the item is saved in the table as well but with a value of 0 (not interested in this product). Three records must always be stored in the table for an order. I tried checkboxes instead of radio, but it was the same problem. Where am I wrong?
HTML form:
<form class="repeater" method="post" action="" enctype="multipart/form-data">
@csrf
<input type="hidden" name="email" value="">
<div class="mb-3 col-lg-2">
<label for="provozovna">Objednávka pro provozovnu...</label>
<select class="form-select" id="provozovna" name="provozovna" required>
<option disabled selected>Vybrat...</option>
<option value="1">Praha</option>
<option value="2">Brno</option>
</select>
</div>
<hr>
<div class="row">
<div class="col-md-6">
<h3>Verze s nikotinem</h3>
<hr>
<div data-repeater-list="group-a">
<div data-repeater-item class="row">
<div class="mb-3 col-lg-6">
<label for="nikotin">Název</label><br>
<img src="" class="avatar-xxs rounded-circle" alt="Blueberry">
<input type="hidden" name="barva" value="blueberry">
Puff House - 800 potáhnutí, Blueberry Ice
<input type="hidden" name="order_items[0][nazev]" value="800-blueberry">
<input type="hidden" name="order_items[0][nikotin]" value="s nikotinem">
</div>
<div class="mb-3 col-lg-4">
<div class="container mt-4">
<div class="btn-toolbar" role="toolbar">
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="order_items[0][mnozstvi]" id="btnBlueberry1" value="12" autocomplete="off">
<label class="btn btn-outline-primary" for="btnBlueberry1">x12</label>
<input type="radio" class="btn-check" name="order_items[0][mnozstvi]" id="btnBlueberry2" value="24" autocomplete="off">
<label class="btn btn-outline-primary" for="btnBlueberry2">x24</label>
<input type="radio" class="btn-check" name="order_items[0][mnozstvi]" id="btnBlueberry3" value="36" autocomplete="off">
<label class="btn btn-outline-primary" for="btnBlueberry3">x36</label>
<input type="radio" class="btn-check" name="order_items[0][mnozstvi]" id="btnBlueberry4" value="48" autocomplete="off">
<label class="btn btn-outline-primary" for="btnBlueberry4">x48</label>
<input type="radio" class="btn-check" name="order_items[0][mnozstvi]" id="btnBlueberry5" value="60" autocomplete="off">
<label class="btn btn-outline-primary" for="btnBlueberry5">x60</label>
</div>
</div>
</div>
</div>
</div>
<div data-repeater-item class="row">
<div class="mb-3 col-lg-6">
<img src="" class="avatar-xxs rounded-circle" alt="Fruit Mix">
<input type="hidden" name="barva" value="fruitmix">
Puff House - 800 potáhnutí, Fruit Mix
<input type="hidden" name="order_items[1][nazev]" value="800-fruitmix">
<input type="hidden" name="order_items[1][nikotin]" value="s nikotinem">
</div>
<div class="mb-3 col-lg-4">
<div class="container">
<div class="btn-toolbar" role="toolbar">
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="order_items[1][mnozstvi]" id="btnFruitmix1" value="12" autocomplete="off">
<label class="btn btn-outline-primary" for="btnFruitmix1">x12</label>
<input type="radio" class="btn-check" name="order_items[1][mnozstvi]" id="btnFruitmix2" value="24" autocomplete="off">
<label class="btn btn-outline-primary" for="btnFruitmix2">x24</label>
<input type="radio" class="btn-check" name="order_items[1][mnozstvi]" id="btnFruitmix3" value="36" autocomplete="off">
<label class="btn btn-outline-primary" for="btnFruitmix3">x36</label>
<input type="radio" class="btn-check" name="order_items[1][mnozstvi]" id="btnFruitmix4" value="48" autocomplete="off">
<label class="btn btn-outline-primary" for="btnFruitmix4">x48</label>
<input type="radio" class="btn-check" name="order_items[1][mnozstvi]" id="btnFruitmix5" value="60" autocomplete="off">
<label class="btn btn-outline-primary" for="btnFruitmix5">x60</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<h3>Verze bez nikotinu</h3>
<hr>
<div data-repeater-list="group-a">
<div data-repeater-item class="row">
<div class="mb-3 col-lg-6">
<label for="nikotin">Název</label><br>
<img src="" class="avatar-xxs rounded-circle" alt="Lychee Ice">
<input type="hidden" name="barva" value="lycheeZero">
Puff House, Lychee Ice ZERO 800+
<input type="hidden" name="order_items[2][nazev]" value="800-lycheeZero">
<input type="hidden" name="order_items[2][nikotin]" value="bez nikotinu">
</div>
<div class="mb-3 col-lg-4">
<div class="container mt-4">
<div class="btn-toolbar" role="toolbar">
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="order_items[2][mnozstvi]" id="btnLycheeZero1" value="12" autocomplete="off">
<label class="btn btn-outline-primary" for="btnLycheeZero1">x12</label>
<input type="radio" class="btn-check" name="order_items[2][mnozstvi]" id="btnLycheeZero2" value="24" autocomplete="off">
<label class="btn btn-outline-primary" for="btnLycheeZero2">x24</label>
<input type="radio" class="btn-check" name="order_items[2][mnozstvi]" id="btnLycheeZero3" value="36" autocomplete="off">
<label class="btn btn-outline-primary" for="btnLycheeZero3">x36</label>
<input type="radio" class="btn-check" name="order_items[2][mnozstvi]" id="btnLycheeZero4" value="48" autocomplete="off">
<label class="btn btn-outline-primary" for="btnLycheeZero4">x48</label>
<input type="radio" class="btn-check" name="order_items[2][mnozstvi]" id="btnLycheeZero5" value="60" autocomplete="off">
<label class="btn btn-outline-primary" for="btnLycheeZero5">x60</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="d-flex justify-content-end">
<input type="submit" class="btn btn-primary" value="Odeslat objednávku"/>
</div>
And my controller looks like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\OrderItem;
class OrderController extends Controller
{
public function processForm(Request $request)
{
// Zpracování formuláře a uložení do databáze
$order = new Order();
$order->provozovna_id = $request->input('provozovna');
$order->email = $request->input('email');
$order->save();
$orderItems = $request->input('order_items');
foreach ($orderItems as $item) {
$orderItem = new OrderItem();
$orderItem->order_id = $order->id;
$orderItem->nazev = $item['nazev'];
$orderItem->nikotin = $item['nikotin'];
$orderItem->mnozstvi = isset($item['mnozstvi']) ? $item['mnozstvi'] : 0;
$orderItem->save();
}
return redirect()->back()->with('success', 'Objednávka byla úspěšně odeslána.');
}
}
Aucun commentaire:
Enregistrer un commentaire