如何查询laravel中的嵌套关​​系

时间:2019-09-05 07:31:00

标签: php laravel

我有3个型号 住所 住宿 roomPricingHistory 我正在通过我的酒店模型对选择该用户的每个用户进行查询,以选择每个房间的最低价格,然后返回该价格,我想从所有房间中选择价格最低的房间,这是我现在的查询:

           $data = Accommodation::with(['city','accommodationRoomsLimited.roomPricingHistorySearch' =>function($query) use($from_date,$to_date){
            $query->whereDate('from_date', '<=', $from_date);
            $query->whereDate('to_date', '>=', $to_date);
// here is where i want to pick the lowest price room and actually do a query on accommodationRoomsLimited based on roompricinghistorysearch result
                }])
                ->whereIn('city_id',$city_id)
                ->whereIn('grade_stars',$stars)
                ->orWhere('accommodation_type_id',$type_id)
                ->get();

这是我在api中当前查询的结果输出:

 {
    "id": 47,
    "operator_id": 12,
    "city_id": 139,
    "accommodation_status_id": 1,
    "child_age": null,
    "infant_age": null,
    "accommodation_provider_id": null,
    "grade_stars": 2,
    "accommodation_type_id": 1,
    "start_price": 1760000,
    "start_discount": 10,

    "english_name": null,
    "english_description": null,
    "english_address": null,
    "rules": null,
    "english_rules": null,
    "supply_team_note": null,
    "phone": null,
    "email": null,
    "cellphone": null,
    "receptionist": null,
    "is_removed": 0,
    "is_recommended": 0,
    "latitude": "32.66108300",
    "longitude": "51.6693880",
    "is_deleted": 0,
    "checkin_time": "14",
    "checkout_time": "12",
    "st_hid": 72,
    "created_at": "2019-05-22 19:20:02",
    "updated_at": "2019-05-25 17:59:06",
    "city": {
        "id": 139,

        "english_name": "Isfahan"
    },
    "accommodation_rooms_limited": [
        {
            "id": 4859,
            "accommodation_id": 47,
            "room_pricing_history_search": {
                "id": 15741,
                "accommodation_room_id": 4859,
                "net_price": null,
                "sales_price": 50,
                "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,
                "foreign_discount_percent": null,
                "from_date": "2019-05-25 00:00:00",
                "to_date": "2019-08-30 23:59:59",
                "is_deleted": 0,
                "created_at": "2019-05-25 13:30:00",
                "updated_at": "2019-05-25 13:30:00"
            }
        },
        {
            "id": 4860,
            "accommodation_id": 47,
            "room_pricing_history_search": {
                "id": 4990,
                "accommodation_room_id": 4860,
                "net_price": null,
                "sales_price": 1760000,
                "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,
                "foreign_discount_percent": null,
                "from_date": "2019-05-25 00:00:00",
                "to_date": "2019-08-30 23:59:59",
                "is_deleted": 0,
                "created_at": "2019-05-25 13:30:00",
                "updated_at": "2019-05-25 13:30:00"
            }
        },
        {
            "id": 4861,
            "accommodation_id": 47,
            "room_pricing_history_search": {
                "id": 4991,
                "accommodation_room_id": 4861,
                "net_price": null,
                "sales_price": 2270000,
                "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,
                "foreign_discount_percent": null,
                "from_date": "2019-05-25 00:00:00",
                "to_date": "2019-08-30 23:59:59",
                "is_deleted": 0,
                "created_at": "2019-05-25 13:30:00",
                "updated_at": "2019-05-25 13:30:00"
            }
        },
        {
            "id": 4862,
            "accommodation_id": 47,
            "room_pricing_history_search": {
                "id": 4992,
                "accommodation_room_id": 4862,
                "net_price": null,
                "sales_price": 2780000,
                "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,
                "foreign_discount_percent": null,
                "from_date": "2019-05-25 00:00:00",
                "to_date": "2019-08-30 23:59:59",
                "is_deleted": 0,
                "created_at": "2019-05-25 13:30:00",
                "updated_at": "2019-05-25 13:30:00"
            }
        }
    ]
},

因此,在我的accommodation_rooms_limited中,我希望根据room_pricing_history_search的sales_price字段,将1个房间和1个结果作为所有房间中最便宜的房间。

1 个答案:

答案 0 :(得分:0)

您是否尝试过下面的查询显示

  <?php         

 $data = Accommodation::with(['city', 'accommodationRoomsLimited.roomPricingHistorySearch' => function($query) use($from_date,$to_date) {
      $query->whereDate('from_date', '<=', $from_date);
      $query->whereDate('to_date', '>=', $to_date);
      $query->OrderBy('sales_price' , 'ASC');
      $query->limit(1);    
    }])
    ->whereIn('city_id',$city_id)
    ->whereIn('grade_stars',$stars)
    ->orWhere('accommodation_type_id',$type_id)
    ->get();