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

farshadf's avatar

is it wise to call your own application api in your controller in loop ??

i have an application that in a controller i am trying to send some room id and some date for each day i want to loop and for each room now the problem is when i run this query below :

  $from_date = $request->get('from_date');
        $to_date = $request->get('to_date');
        $room_ids[] = $request->get('room_ids');

        $period = CarbonPeriod::create($from_date, $to_date);
        $dates = $period->toArray();

        for ($i = 0; $i < count($dates); $i++) {

            for ($f = 0; $f < count($room_ids); $f++) {

                /****************************************
                 * Looping for the Number of Rooms User Given
                 *****************************************/
                $room_price = RoomPricingHistory::with('accommodationRoom', 'accommodationRoom.roomCapacityHistoryLast')
                    ->where('accommodation_room_id', $room_ids[$i])
                    ->whereDate('from_date', '<=', $dates[$f])
                    ->whereDate('to_date', '>=', $dates[$f])
                    ->get()->sortBy('created_at');
                var_dump('time');

                $data_array[] = $room_price[$i];
                $data_collection = collect($data_array);

                $sum_price[] = $room_price[$i]->sales_price;
                $sum_half_board[] = $room_price[$i]->half_charge_price;
                $sum_full_board[] = $room_price[$i]->full_board_price;
            }
        }
        foreach ($room_price as $item) {
            if ($item->accommodationRoom->roomCapacityHistoryLast->capacity > 10 ?? null) {
                $check_capacity = 0;
            } else {
                $check_capacity = 1;
            }
        }
        $night_count = count($dates);
        $sum = array_sum($sum_price);
        $sum_half = array_sum($sum_half_board);
        $sum_full = array_sum($sum_full_board);

        $alldata = $sum . ',' . $sum_half . ',' . $sum_full . ',' . $night_count . ',' . $check_capacity;
        return RoomDetailResource::collection($data_collection)->sum($alldata);

i get this error :

  "message": "Undefined offset: 1",
    "status_code": 500,

i think its because 1 loop runs more than once now my question is call i make 1 for and make an api from it and then in another function call that api whithin a loop and get the result ?? is it wise to do such or we sould not call our own api in our own project ?? or if there is any way to solve this error above ??

0 likes
1 reply
tykus's avatar

Why would you pay the cost of additional requests to the same application when you already have access to the data it would return anyway?

It would seem there is an opportunity to extract the common implementation from the controller than serves the API endpoint to another class that can be called from either of the endpoints; the data retrieved should be the same in both cases.

Please or to participate in this conversation.