다음과 같이 반환 할 개체가 있습니다. (dd 결과)
Collection {#1421 ▼
#items: array:2 [▼
3943 => Collection {#1419 ▼
#items: array:2 [▼
0 => RoomPricingHistory {#923 ▼
#fillable: array:19 [▶]
#connection: "mysql"
#table: "room_pricing_histories"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:26 [▶]
#original: array:26 [▼
"id" => 4132
"accommodation_room_id" => 3943
"net_price" => null
"board_price" => null
"foreign_board_price" => null
"sales_price" => 4200000
"extra_bed_price" => null
"half_charge_price" => null
"half_board_price" => null
"full_board_price" => null
"foreign_net_price" => null
"foreign_sales_price" => null
"foreign_extra_bed_price" => null
"foreign_half_charge_price" => null
"foreign_half_board_price" => null
"foreign_full_board_price" => null
"operator_id" => 11
"commission_percent" => null
"foreign_commission_percent" => null
"discount_percent" => 10.0
"foreign_discount_percent" => null
"from_date" => "2019-05-25 00:00:00"
"to_date" => "2029-08-30 23:59:59"
"is_deleted" => 0
"created_at" => "2019-05-25 13:30:00"
"updated_at" => "2019-05-25 13:30:00"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
#enableLoggingModelsEvents: true
#oldAttributes: []
}
1 => RoomPricingHistory {#1042 ▶}
]
}
3944 => Collection {#1420 ▶}
]
}
그리고 아래와 같은 매핑 된 데이터가 있습니다.
Collection {#1422 ▼
#items: array:2 [▼
3943 => 8400000
3944 => 400
]
}
이제 내가하고 싶은 것은 그들이 가지고있는 id에 따른 가격의 합계 인 매핑 된 데이터를 반환하는 것이므로 각 항목의 결과는 아래와 같이 가격의 합계를 갖습니다.
{
//room pricing history
id:4132
room_id:3943
sum_of_prices :8400000
},
{
//room pricing history
id:4133
room_id:3944
sum_of_prices :600
}
참고 : 나는 현재 자원을 사용하고 있지만 예를 들어 동일한 ID 또는 무언가가있는이 조건으로 자신의 개체에 데이터를 푸시하는 방법을 모릅니다.
내 방법은 다음과 같습니다.
$from_date = $request->get('from_date');
$to_date = $request->get('to_date');
$acc_id = $request->get('acc_id');
$room_price = [];
$period = CarbonPeriod::create($from_date, $to_date);
$dates = $period->toArray();
$room_ids = AccommodationRoom::where('accommodation_id',$acc_id)->pluck('id')->toArray();
for ($f = 0; $f < count($room_ids); $f++) {
for ($i = 0; $i < count($dates); $i++) {
/****************************************
* Looping for the Number of Rooms User Given
*****************************************/
$room_price[] = RoomPricingHistory::with('accommodationRoom', 'accommodationRoom.roomCapacityHistoryLast')
->where('accommodation_room_id', $room_ids[$f])
->whereDate('from_date', '<=', $dates[$i])
->whereDate('to_date', '>=', $dates[$i])
->get()->sortByDesc('created_at')
->take(1);
}
}
$room = collect($room_price);
$room_collection= $room->flatten();
$sums = $detailed->mapWithKeys(function ($group, $key) {
return [$key => $group->sum('sales_price')];
});
return RoomDetailResource::collection($room_collection);
그룹의 합계를 구하는 방법을 알아 낸 이전 질문을 기반으로 확장하여 원하는 추가 데이터를 포함하고 map
대신 사용할 수 있습니다 .
$new = $detailed->map(function ($group, $key) {
return (object) [
'id' => $group->first()->id,
'room_id' => $key,
'sum_of_prices' => $group->sum('sales_price'),
];
})->values();
values
마지막에 호출을 던져 키를 재설정하여 JSON 출력이 객체가 아닌 배열이되고 예제 출력이 키를 사용하지 않는 것 같습니다. 이러한 객체를 'room_id'로 입력하려면을 호출하지 마십시오 values
.
당신이 가진 API 리소스가 특정 데이터를 기대하고 있기 때문에 실제로 무엇을 하려는지 잘 모르겠습니다. 임의의 데이터를 Eloquent 컬렉션에 혼합하려는 것 같습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다