Title: how to validate nested arrays in laravel validations and merge it
Tags: php;laravel
Question: I have a nested array in my body of request which I want to replace it with some value like below :
&quot;sender&quot;:
&quot;name&quot;:&quot;sender-name&quot;,
&quot;contact&quot;:
{
&quot;postal_code&quot;:&quot;123453222&quot;,
&quot;city&quot;:&quot;2201&quot;,
&quot;latitude&quot;: 35.73845582400794,
&quot;longitude&quot;: 51.40889167785645,
.
.
.
now what is the problem is that I want to merge the city field and just the city field, not all the contact. so what I have done so far is like below :
protected function prepareForValidation()
$this-&gt;merge(['sender' =&gt; ['contact' =&gt; 'city' =&gt; $somevalue]], ['receiver' =&gt; ['contact' =&gt; ['city' =&gt; $somevalue]]]]);
}
but the problem here is this wants me to pass all the contact array which I don't want to merge and replace all the contact array I just want to merge the city field in that.


Here is another answer: 'product_size' =&gt; 'required|array',
'product_size.*' =&gt; 'required:string',
you can use look like this

Here is another answer: $rules = [
'sender' = 'required|array',
'sender.name' = 'required|string',
'sender.contact' = 'required|array',
'sender.contact.postal_code' = 'required|int',

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:03/06/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><laravel>', 'Title': 'how to validate nested arrays in laravel validations and merge it', 'ViewCount': 214, 'id': 66503255, 'user_AboutMe': '', 'user_AccountId': 4089204.0, 'user_DisplayName': 'Farshad', 'user_Reputation': 1623.0, 'user_Views': 364.0, 'user_id': 3357687.0}}