Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kuns25's avatar

How to modify date format of request array in laravel form request validation

Hi, My validation rule is simple 'legs.*.date_of_service' => ['required','date_format:m-d-Y'], I am trying to modify date format from every element in array once the validation is passed in my form request.

    public function passedValidation()
    {
  // how do I get the each date from array of date of service
        $this->merge([ 
           'legs.*.date_of_service'=> Carbon::createFromFormat('m-d-Y', $each_date_of_service)->format('Y-m-d')
        ]);
    }

Thanks

0 likes
5 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Try this:

$this->merge([
    'legs' => collect($this->input('legs'))->map(function($leg) {
		  $leg['date_of_service'] = Carbon::createFromFormat('m-d-Y', $leg['date_of_service'])->format('Y-m-d');
          return $leg;
    })->toArray()
]);

I tried with simple array, without an additional key like you have date_of_service so just legs.* for example and it worked.

Let me know if this works too.

1 like
kuns25's avatar

@Nakov Hey, Can you me with one more validation, I am trying to put conditional required validation on the array. if index is greater than 0, so if legs[1] , leg[2].. and so on all these arrays will have required validation of legs.*['trip_format'] => ['required''] . and leg[0] will not have this validation

Nakov's avatar

@kuns25 that's a new requirement. You can start a new thread for that, maybe someone can help if they faced it. I don't know from top of my head and I can't try it now.

Please or to participate in this conversation.