Note: Storing 'details' as json field in database
Form Request Validation Code:
public function rules(Request $request)
{
return [
'name' => 'required | string',
'details.subscriptions' => 'required | array',
'details.subscriptions.*' => 'nullable | string',
'details.size' => 'nullable | array',
'details.size.*' => 'nullable | string',
'details.notes' => 'nullable | array',
'details.notes.*' => 'nullable | string',
'details.price' => 'nullable | array',
'details.price.*' => 'nullable | double',
];
}
protected function prepareForValidation()
{
$this->merge([
'name' => ucwords($this->name),
]);
}
Validated result - what I get:
{
"name":"Some Name",
"details":{
"subscriptions":["1"],
"size":[null,null],
"notes":["10 pcs",null],
"price":[100,null]}
}
What I want to get:
{
"name":"Some Name",
"details":{
"subscriptions":["1"],
"notes":["10 pcs"],
"price":[100]
}
}
Pls help, I am stuck.
Tried this in controller store method:
$validated = $request->validated();
$collection = collect($validated);
$size = array_filter($validated['details']['size'], function (
$value
) {
return !is_null($value);
});
This returns an empty array. But I am unable to figure out what to do next.