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

pllaguno's avatar

making api controller batch update

I need to make an API call that will update multiple instances of a model at the same time.

at the moment my controller is just

public function store(Request $request)
    {
        dd($request->getContent());
    }

and when my client sends a json object

{"VehicleID":"95d8f9a1-0791-4fb7-add7-a84e727e28a5","Latitude":25.3958788,"Longitud":-100.1214968,"ActivityDateTime":"2019-05-08T19:41:48","EventSubType":"SMDP_EVENT_VEHICLE_MOVED_WITH_IGIOFF","Speed":0},{"VehicleID":"64d2068a-907a-4ab2-bfe3-100122acbeae","Latitude":25.8705263,"Longitud":-100.252277,"ActivityDateTime":"2019-08-03T11:00:00","EventSubType":"SMDP_EVENT_GRACEFUL_SHUTDOWN","Speed":0}

what should my controller do so that i can update them in a single batch?

the primary key is the VehicleID which is a GUID.

is there a way to do it on a single call or should i make multiple calls? i ned to do about 25 rows updated per call. and this is done every 3 minutes,that is why i want to make the networking easier and do batch updates.

0 likes
1 reply
mabdullahsari's avatar

The only way you can carry out a mass update is if the models you are about to update receive the same data ie. if you have different update data for each model then there is no other way than simply updating them one by one.

If there are a few in common which need to be updated with the same data, you can group them and carry out separate mass updates to save DB calls.

If you feel like the updates happen way too frequently, you can think of a particular updating strategy like for example keeping a queue of the data the client has sent and utilizing the most relevant out of it every X amount of minutes.

Please or to participate in this conversation.