猿问

使用嵌套关系的结果查询关系

在这里,我有下面的代码,我想过滤我的酒店并为每家酒店选择最低价格的房间:


 $city_id = $request->get('city_id');

            $stars = $request->get('grade_stars');

            $type_id = $request->get('accommodation_type_id');

            $from_date = $request->get('from_date');

            $to_date = $request->get('to_date');

            $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);

                }])

                ->whereIn('city_id',$city_id)

                ->whereIn('grade_stars',$stars)

                ->orWhere('accommodation_type_id',$type_id)

                ->paginate(10);

            return $data;

这是我的住宿模式:


    public function accommodationRoomsLimited()

{

    return $this->Hasmany(AccommodationRoom::class)->select('id', 'accommodation_id');

}

在我的住宿房间模型中:


  public function roomPricingHistorySearch()

{

    return $this->hasOne(RoomPricingHistory::class,'accommodation_room_id','id')->orderBy('sales_price','ASC');

}

我有一个叫做房间定价历史的表,它记录了每天的房间价格,这是我在 api 中的输出,我想在 room_pricing_history_search 中选择 1 个具有最低 sales_price 的房间:


"accommodation_rooms_limited": [

        {

            "id": 4859,

            "accommodation_id": 47,

            "room_pricing_history_search": {

                "id": 15741,

                "accommodation_room_id": 4859,

                "net_price": null,

                "sales_price": 50,


                "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,


                "created_at": "2019-05-25 13:30:00",

                "updated_at": "2019-05-25 13:30:00"

            }

        },


弑天下
浏览 122回答 2
2回答

Smart猫小萌

从选择房间而不是酒店开始,然后按酒店分组可能更简单。您可以使用原始表达式来获得最低房价:$raw = DB::raw('min(price) as min_price');然后从 rooms 表中选择并按酒店 id 分组:Room::join('hotels', 'hotels.id' ,'=', 'rooms.hotel_id')&nbsp; &nbsp;->whereDate('rooms.from_date', '<=', $from_date)&nbsp; &nbsp;->whereDate('rooms.to_date', '>=', $to_date)&nbsp; &nbsp;->select('hotels.*', $raw)&nbsp; &nbsp;->groupBy('hotels.id')&nbsp; &nbsp;->get()您可能也对Jonathan Reinink 最近的演讲 Eloquent Performance Patterns感兴趣。他谈到了与您类似的查询,看起来他谈到的拉取请求将使此类查询更容易已合并到 Laravel 6.0 中。所以从 Laravel 6.0 开始,你应该可以做这样的事情:return Hotel::addSelect(['lowest_price' => function ($query) {&nbsp; &nbsp; $query->select('price')&nbsp; &nbsp; &nbsp; &nbsp; ->from('rooms')&nbsp; &nbsp; &nbsp; &nbsp; ->whereColumn('hotel_id', 'hotels.id')&nbsp; &nbsp; &nbsp; &nbsp; ->orderBy('price', 'asc')&nbsp; &nbsp; &nbsp; &nbsp; ->limit(1);}])->get();

翻过高山走不出你

像你这样的声音需要在阅读了orderBy和take雄辩的文档。检查Adding Additional Constraints部分:在https://laravel.com/docs/5.8/eloquent#retrieving-models
随时随地看视频慕课网APP
我要回答