I think this would do it
return RoomDetailResource::collect($result);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i have an api that returns an object as wrapper data in result like below :
{
data: {
totalSalesPrice: 0,
totalDiscount: 0,
finalPrice: 0
}
}
but i want it to be an array like below :
{
data: [
totalSalesPrice: 0,
totalDiscount: 0,
finalPrice: 0
]
}
or maybe even like below :
{
data: [
{
totalSalesPrice: 0,
totalDiscount: 0,
finalPrice: 0
}
]
}
how can i achive that ?? and here is my method :
public function getPriceDetails($bookingId)
{
$booking = AccommodationBooking::where('id', $bookingId)->with('accommodationRoom')->first();
$roomsOfBook = DB::table('accommodation_booking_accommodation_room')->where('accommodation_booking_id', $bookingId)->get();
$result = [];
foreach ($roomsOfBook as $room) {
$roomPricingHistory = RoomPricingHistory::where('accommodation_room_id', $room->accommodation_room_id)
->whereDate('from_date', '<=', $booking->from_date)
->whereDate('to_date', '>=', $booking->to_date)
->orderBy('created_at', 'desc')
->first();
$result[] = $roomPricingHistory;
}
$totalSalesPrice = collect($result)->sum('sales_price');
$totalDiscount = 0;
$finalPrice = $totalSalesPrice;
$result['totalSalesPrice'] = $totalSalesPrice;
$result['totalDiscount'] = $totalDiscount;
$result['finalPrice'] = $finalPrice;
return new RoomDetailResource($result);
}
Please or to participate in this conversation.