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

hellopeter13's avatar

Accepting multiple values from json to controller for validation

Hi, So i've a requirement, from frontend we are sending data multiple values with same name.

I requirement, while adding new class, we can add multiple students who are part of this class!

Now some students will have different number of sessions, other students will have different number of classes.

example

student_name: Mark student_classes: 5

student_name: John student_classes: 10

Now we are trying to create a validation rule for this but it is not working

$rules = [
            'student_name.*'        =>  'required|max:10',
            'student_classes.*'          =>  'required|max:50',
];

validation is simply ignoring these values when we are sending details via postman

0 likes
6 replies
Nakov's avatar

You cannot send the same attribute name multiple times in the same request, the latest will overwrite the previous values. So if you remove the .* from the validations you will be able to validate the data, but that will validate just the last item..

So you can group them like this for example:

{students: [{name: 'Mark', 'classes': 5}, {name: 'John', 'classes': 10}]}

and then validate them:

$rules = [
            'students.*.name'        =>  'required|max:10',
            'students.*.classes'          =>  'required|max:50',
];
hellopeter13's avatar

@Nakov I tried on postman, but laravel seems to be ignoring students.*.name field all together, any alternative?

Nakov's avatar

@hellopeter13 you obviously are doing something wrong.. and unfortunately I cannot say without seeing what you are trying.

You can always debug what you are sending in the request.. if you are hitting the validation rules at all or no matter what you do you always have the same response..

hellopeter13's avatar

@Nakov can you tell me about how I can send requests via postman for students.*.name ? maybe this will help me, i feel i'm doing something wrong in postman

Nakov's avatar

@hellopeter13 I already gave you above. The body of the request should contain this object:

{students: [{name: 'Mark', 'classes': 5}, {name: 'John', 'classes': 10}]}

and set the Content-type to application/json

1 like
krisi_gjika's avatar

@hellopeter13 using postman you can send {students: [{name: 'Mark', 'classes': 5}, {name: 'John', 'classes': 10}]} as json payload, or using formData:

students[0][name]:
students[0][classes]:
students[1][name]:
students[1][classes]:

Please or to participate in this conversation.