I’m working on a multi-step form in Laravel to handle order submissions. The form allows users to request various items from different categories, and I’m encountering an issue where some items are missing the quantity key in the submitted data. Despite setting a default quantity value for these items, the error persists.
In my application, the order submission involves iterating over the requested items and ensuring that each item has a valid quantity before processing it. However, the log file shows that some items still have a quantity key missing or set to null, even after attempting to set a default value
CODE
Here is the category table
id | category_name | created_at | updated_at
Here is the product table
id | category_id (fk) | product_name | updated_at | created_at
Here is the inventory table
id | avaliability | status | weight | sku | product_id (fk) | area | monetary_value |
donated_value | created_at | updated_at
Category Model
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use AppModelsProducts;
class Category extends Model
{
protected $table = 'category';
protected $fillable = ['category_name'];
use HasFactory;
public function products() {
return $this ->hasMany(Products::class);
}
}
Products Model
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use AppModelsCategory;
class Products extends Model
{
protected $table = 'products';
use HasFactory;
protected $fillable = ['product_name', 'category_id'];
public function category() {
return $this->belongsTo(Category::class);
}
public function inventories()
{
return $this->hasMany(Inventory::class,'product_id');
}
}
Inventory Model
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use AppModelsProducts;
class Inventory extends Model
{
protected $table = 'inventory';
protected $fillable = [
'sku',
'product_id',
'created_at',
'updated_at',
];
// Define the relationship to the Product model
public function product()
{
return $this->belongsTo(Products::class,'product_id');
}
use HasFactory;
}
HTML FORM
<h4>Order Section</h4>
<section>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Order Section</h4>
</div>
<div class="card-body family-demo-scrollbar">
<div class="table-responsive ">
<table id="example" class="display" style="min-width: 845px;">
<thead>
<tr>
<th>AVALIABILITY</th>
<th>STATUS</th>
<th>CATEGORY</th>
<th>ITEM NAME</th>
<th>REQUESTED</th>
</tr>
</thead>
<tbody>
@foreach($fetchInventoryData as $data)
@foreach($data->products as $product)
@foreach($product->inventories as $inventory)
<tr>
<td>{{$inventory->avaliability}}</td>
<td style="color: {{ $inventory->status === 'IN STOCK' ? 'green' : 'red' }};">{{ $inventory->status }}</td>
<td>{{ $data->category_name }}</td>
<td>{{ $product->product_name }}</td>
<td>
<!-- <input type="number" name="" id="" min="0" class="form-control"> -->
<input type="number" name="requested[{{ $product->id }}][quantity]" min="0" class="form-control">
<input type="hidden" name="requested[{{ $product->id }}][category]" value="{{ $data->category_name }}">
<input type="hidden" name="requested[{{ $product->id }}][requestedItem]" value="{{ $product->product_name }}">
</td>
</tr>
@endforeach
@endforeach
@endforeach
</tbody>
<tfoot>
<tr>
<th>AVALIABILITY</th>
<th>STATUS</th>
<th>CATEGORY</th>
<th>ITEM NAME</th>
<th>REQUESTED</th>
</tr>
</tfoot>
</table>
<br><br>
</div>
</div>
</div>
</div>
</div>
</section>
CONTROLLER
// handling the third section of the multi step form for request order
$requestedItems = $req->input('requested');
// Validate or handle missing quantity values (consider front-end validation)
foreach ($requestedItems as &$itemData) {
if (!isset($itemData['quantity']) || is_null($itemData['quantity'])) {
$itemData['quantity'] = 0; // Set a default value (optional)
// Display a user-friendly error message (front-end)
}
}
unset($itemData); // Unset reference after loop
$categoryTotals = $this->calculateCategoryTotals($requestedItems);
foreach ($requestedItems as $itemData) {
insertOrderItem($itemData, $categoryTotals[$itemData['category']], now(), $familyId);
}
function calculateCategoryTotals($requestedItems) {
$categoryTotals = [];
foreach ($requestedItems as $itemData) {
$category = $itemData['category'];
if (!isset($categoryTotals[$category])) {
$categoryTotals[$category] = 0;
}
$categoryTotals[$category] += $itemData['quantity'];
}
return $categoryTotals;
}
function insertOrderItem($itemData, $categoryTotal, $dateRequested, $familyId) {
DB::table('requestorder')->insert([
'category' => $itemData['category'],
'categoryTotal' => $categoryTotal,
'requestedItem' => $itemData['requestedItem'],
'amountNeeded' => $itemData['quantity'],
'daterequested' => $dateRequested,
'family_id' => $familyId,
]);
}
ERROR LOGS
[2024-06-28 22:19:25] local.INFO: Requested Items: [{"quantity":null,"category":"BATH","requestedItem":"Hangers"},{"quantity":null,"category":"Miscellaneous","requestedItem":"Lamp"},{"quantity":null,"category":"Miscellaneous","requestedItem":"Lg Rug"},{"quantity":null,"category":"Miscellaneous","requestedItem":"Sm Rug"},{"quantity":null,"category":"Miscellaneous","requestedItem":"TV Tray"},{"quantity":null,"category":"Miscellaneous","requestedItem":"Folding table"},{"quantity":null,"category":"Miscellaneous","requestedItem":"Folding chairs"},{"quantity":"1","category":"bedding","requestedItem":"sheet set"},{"quantity":"2","category":"bedding","requestedItem":"Bed Frame"},{"quantity":"3","category":"bedding","requestedItem":"Comforter"}]
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":null,"category":"BATH","requestedItem":"Hangers"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"BATH","requestedItem":"Hangers"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":null,"category":"Miscellaneous","requestedItem":"Lamp"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Lamp"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":null,"category":"Miscellaneous","requestedItem":"Lg Rug"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Lg Rug"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":null,"category":"Miscellaneous","requestedItem":"Sm Rug"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Sm Rug"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":null,"category":"Miscellaneous","requestedItem":"TV Tray"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"TV Tray"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":null,"category":"Miscellaneous","requestedItem":"Folding table"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Folding table"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":null,"category":"Miscellaneous","requestedItem":"Folding chairs"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Folding chairs"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":"1","category":"bedding","requestedItem":"sheet set"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":"2","category":"bedding","requestedItem":"Bed Frame"}
[2024-06-28 22:19:25] local.INFO: Item Data: {"quantity":"3","category":"bedding","requestedItem":"Comforter"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"BATH","requestedItem":"Hangers"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Lamp"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Lg Rug"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Sm Rug"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"TV Tray"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Folding table"}
[2024-06-28 22:19:25] local.ERROR: Quantity key is missing for requested item: {"quantity":null,"category":"Miscellaneous","requestedItem":"Folding chairs"}
[2024-06-28 22:19:25] local.INFO: Family ID below pdfdata : {"family_id":372}