Laravel 5.6 - 如何获取房间交易的最后X个客人姓名?

问题描述 投票:4回答:1

我无法弄清楚如何为以下场景构建有效的Eloquent查询。

用户可以住在很多地方,如房间,公寓,家庭,所以我们有一个多态的stayable_locations表,但我们只关注这个表的房间stayable_type。当工作人员点击房间时,我们希望显示所有可用的房间交易(如果有的话可以从room_deals表中获得)以及每个房间交易的最后3位客人(如果有的话)。

试图通过eloquent从下表中获取此输出:

Room 111 (desired output for room deals and guests below)
- Room Deal #1 -> Able, Kane, Eve
- Room Deal #2 -> Eve, Adam

------------------------------------------
$room = Room::where('id',111)->first(); // room 111
// Eloquent query, not sure how to setup model relations correctly
// To get last 3 guest names per room deal [if any] in an efficient query
$room->roomDeals()->withSpecificRoomDealLast3RoomGuestNamesIfAny()->get();

这是表结构:

stayable_locations table [polymorphic]:
id | stayable_id | stayable_type | room_deal_id | room_guest_id
----------------------------------------------------------------
1  |     111     |     room      |       0      | 3 (Steve no room deal)
2  |     111     |     room      |       1      | 1 (Adam room deal) 
3  |     111     |     room      |       1      | 2 (Eve room deal)
4  |     111     |     room      |       1      | 4 (Kane room deal)
5  |     111     |     room      |       1      | 5 (Able room deal)
6  |     111     |     room      |       2      | 1 (Adam room deal)
7  |     111     |     room      |       2      | 2 (Eve room deal)

room_deals table:
id | room_id | room_deal
-----------------------
1  |   111   | Deal A
2  |   111   | Deal B


users table:
id | name
------------
1  | Adam
2  | Eve
3  | Steve
4  | Kane
5  | Able

更新:显示各自的型号

用户模型:

class User extends Authenticatable {
    public function stayableLocations() {
      return $this->morphMany('App\StayableLocation', 'stayable');
    }
}

RoomDeal型号:

class RoomDeal extends Model {
    public function room() {
        return $this->belongsTo('App\Room');
    }

    public function guests() {
        return $this->belongsToMany('App\User', 'stayable_locations', 'room_deal_id', 'room_guest_id');
    }
}

StayableLocation模型:

class StayableLocation extends Model {
    public function stayable() {
        return $this->morphTo();
    }

    public function room() {
        return $this->belongsTo('App\Room', 'stayable_id');
    }
}

房型号:

class Room extends Model {
  public function stayableLocations() {
    return $this->morphMany('App\StayableLocation', 'stayable');
  }

  public function roomDeals() {
      return $this->hasMany('App\RoomDeal');
  }
}

知道如何通过有效的雄辩查询获得所需的输出吗?

php laravel eloquent laravel-5.5 laravel-5.6
1个回答
1
投票

我从我的问题中的帮助评论中找到了它。开始了:

  1. Laravel没有开箱即用的(see here)所以我们必须使用第三方包。
  2. 按链接说明安装staudenmeir/eloquent-eager-limit软件包,并按照使用示例进行操作。
  3. 这是需要改变以上[仍然使用上面相同的定义关系下面...],只是添加了use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
class User extends Authenticatable {
  use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
  ... 
}

class RoomDeal extends Model {
  use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
  ...
}
  1. 借助于评论者和软件包帮助,通过eloquent使用嵌套限制进行查询:
$room = Room::find(111);

$deals3Guests = $room->roomDeals()                                    // query deals
                ->with(['guests' => function($query) {                // eager load guests
                    $query->orderBy('stayable_locations.id', 'desc')  // get latest guests
                          ->limit(3);                                // limit to 3
                }])
                ->get();
© www.soinside.com 2019 - 2024. All rights reserved.