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

jayson.inf@gmail.com's avatar

Restfull laravel good pratice single or multi requests

I'm developing restfull software using laravel and react.

What is better, make many request or one request with array of object to persist data into db?

I have two tables questions and answers.

I was trying to save the answers sending array of object in laravel. However, it's hard to use the laravel request validators. Even I use validator array in laravel if one object is not correct is hard to specif to frontend wich one is wrong. Another problem, if I use unique laravel only validate into db, not inside my array of object

I was trying to convice the frontend developers that is better using one request for each answer, but they still saying is better send array of all answer and save all at once.

Wich option is better, many request or one request with array of object ?

If you can send link or material that I can study and understand better, I apreciate.

0 likes
1 reply
martinbean's avatar
Level 80

@jayson It’s probably better to send one payload that contains a question and its answers. Sending a question and its answers as individual requests may lead to data inconsistency (i.e. question saved, and only some answers saved because of validation errors, or an error because the question failed to save and then trying to save each answer throws an error because the parent question doesn’t exist). It’s also going to add time to your request if you’re waiting for multiple API calls to resolve behind the scenes.

Laravel validation does support validating arrays (https://laravel.com/docs/master/validation#validating-arrays). If you have a question with multiple answers, and each answer requires a value, then you could validate that like this:

'answers' => 'required|array|min:1',
'answers.*.name' => 'required|distinct|string',

You’ll then get error messages back in a similar format so you can show errors for specific answers against that particular answer.

Please or to participate in this conversation.