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

gitwithravish's avatar

How to pass request data in different format from Request class to Controller

My controller is expected to receive data that will be added to 3 different models. Data is coming as a one dimensional array. What I want to do is validate that data in my Request class and then transform it to two dimensional array,change some attribute names and send it to the controller. Is there any dedicated way to do this in request class?

INCOMING DATA

[
	'data_1' => 'v1',
	'data_2' => 'v2',
	'data_3' => 'v3',
	'data_4' => 'v4',
	'data_5' => 'v5',
	'data_6' => 'v6',
]

WANT TO RECEIVE IN CONTROLLER LIKE FOLLOWING

//want to change array structure
//want to change name of all properties
[
	'group1' => [
		'd1' => 'v1',
		'd2' => 'v2',
		'd3' => 'v3',
	],

	'group2' => [
		'd4' => 'v4',
		'd5' => 'v5',
		'd6' => 'v6',
	]
]
0 likes
5 replies
tykus's avatar

You would need to find some way of grouping the related data (which is not immediately apparent given the example you have provided). You also provide no keys in the original data.

There is nothing baked into the Request class to transform the incoming Request data, but the class is Macroable, so nothing prevents you from writing the functionality to perform that step.

1 like
tykus's avatar

Or modify the form input names to group the data submitted in the request:

<input type="text" name="group1[data1]" />
<input type="text" name="group1[data2]" />
// ...
<input type="text" name="group2[data4]" />
// ... etc
gitwithravish's avatar

@tykus The form is big and constructed in a vue component. I don't think this is a good idea for the current situation, but thanks though ! I did not know that it is possible to group elements like that.

gitwithravish's avatar

What I have done as if now is, created a method prettify in my controller which restructures the data before controller methods use it.

Please or to participate in this conversation.