I've the input function for outbound materials from warehouse like so :
[
"movement_date" => "2022-09-08",
"type" => "1",
"transaction_number" => "OUTBOUND-001",
"remarks" => "Moved to another warehouse",
"items" => [
0 => [
"material_id" => "47",
"uom" => "Kilogram",
"qty" => "1000",
],
1 => [
"material_id" => "16",
"uom" => "Kilogram",
"qty" => "500",
],
],
]
In FormRequest i tried to check the remaining stocks for each materials like so :
return [
'type' => 'required',
'movement_date' => ['required','date', function ($attribute, $value, $fail) {
if ($value > Carbon::now()->format('Y-m-d')) $fail('Invalid date '.Carbon::now()->format('Y-m-d'));
}],
'items' => 'required|array',
'items.*.material_id' => 'required',
'items.*.qty' => ['required', function ($attribute, $value, $fail) {
$stock = \App\Models\MaterialStock::where('material_id', 'I need material id from material_id field here')->first();
$stock === null ? $balance = 0 : $balance = $stock->qty;
if ($balance < $value) $fail('Insufficient stock');
}],
];
The question is, how do i get the "material_id" value when i'm trying to validate the qty field ?