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

Laravel 5.6 - How to fetch last X guest names for room deal?

我不知道如何为以下场景构建高效的 Eloquent 查询。

用户可以停留在很多地方,比如房间、公寓、家里,所以我们有一个多态的 stayable_locations table,但我们只关注这个房间 stayable_type table。当工作人员单击一个房间时,我们要显示所有可用的房间优惠(如果 room_deals table 中有可用的房间优惠)以及每个房间优惠的最后 3 位客人(如果有的话)。

尝试通过 eloquent 从以下 table 中获取此输出:

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

这里是 table 结构:

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');
    }
}

客房交易型号:

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');
  }
}

知道如何通过高效的 eloquent 查询获得所需的输出吗?

我从问题中的帮助评论中弄明白了。我们开始吧:

  1. Laravel 没有现成的 (see here) 所以我们必须使用第三方包。
  2. 按照 link 说明安装 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();