我有一个酒店模型,有许多房间可以占用.我应该如何查询:
酒店列表
查询:
$hotels = Hotel::where('foo',$bar)
->withCount('rooms')
->withCount(['rooms' => function ($query) {
$query->where('status', 'Occupied');
}])
->get();
Run Code Online (Sandbox Code Playgroud)
结果 :
$hotel->rooms_count给出占用房间的数量,这是最后一个withCount表达式.
我想要得到什么
$hotel->rooms_count 作为每个酒店的客房数量
$hotel->occupied_rooms_count 作为每个酒店的占用房间数
作为第二个的别名withcount:
题
有没有一种方法,以别名第二withCount的房?