Mar 3, 2019
0
Level 4
form request, validate array and store the validated() in a model
Hello, I encounter a problem with the form request validation,
I want to validate the surfaces and store it with the realtionship createMany()
POST:
{
"data": {
"type": "properties",
"attributes": {
"type_transaction": "SALE",
"property_type": "HOUSE",
"property_subtype": "FARM",
"surfaces": [
{
"field": "bedroom",
"value": 23.00,
"unit": "M2"
},
{
"field": "living",
"value": 43.00,
"unit": "M2"
}
]
}
}
}
PropertyRequest.php
return [
...
'surfaces' => 'array',
'surfaces.*.field' => 'string',
'surfaces.*.value' => 'numeric',
'surfaces.*.unit' => 'string',
];
I get this results when I dump the validated request
$validated = $request->validated()
dd($validated);
array:7 [▼
...
"surfaces_0" => array:3 [▼
"field" => "bedroom"
"value" => 17.0
"unit" => "M2"
]
"surfaces_1" => array:3 [▼
"field" => "living"
"value" => 43.0
"unit" => "M2"
]
]
Is there a way to execute createMany() with this result ? I would like to write something like this:
$porperty->surfaces()->createMany($validated['surfaces']);
I have think to use multiple validator (one for each model (surfaces, nearby, rooms, ...) but honestly I don't know how to handle it, if you have suggestion on how I should do it, share it :)
Thank you !
Please or to participate in this conversation.