我需要用withcount来计算orderpassenger
$orders = $this->orderRepository
->search($filters, true)
->sort($sort)
->select([
'id',
'code',
'pro_periods_id',
'quantity_adult_single',
'quantity_adult_double',
'quantity_adult_triple',
'quantity_child_bed',
'quantity_child_no_bed',
'quantity_infant',
'created_at',
'created_by'
])
->with(['orderPassengers' => function($sub_query){
$sub_query->select([
'id',
'pro_orders_id',
'pro_rooms_id',
'title_name',
'first_name_en',
'last_name_en',
'birth_date'
])
->with(['room' => function($sub_query){
$sub_query->select([
'id',
'pro_periods_id',
'room_number',
'room_type',
'extra_bed'
]);
}]);
}])
->withCount('orderPassengers');
这是我的结果 enter image description here
withcount我什么也没得到。
如何获取orderPassenger_count。
答案 0 :(得分:0)
尝试一下:
$orders = $this->orderRepository
->search($filters, true)
->sort($sort)
->select([
'id',
'code',
'pro_periods_id',
'quantity_adult_single',
'quantity_adult_double',
'quantity_adult_triple',
'quantity_child_bed',
'quantity_child_no_bed',
'quantity_infant',
'created_at',
'created_by'
])
->withCount(['orderPassengers' => function($sub_query){
$sub_query->select([
'id',
'pro_orders_id',
'pro_rooms_id',
'title_name',
'first_name_en',
'last_name_en',
'birth_date'
])
->with(['room' => function($sub_query){
$sub_query->select([
'id',
'pro_periods_id',
'room_number',
'room_type',
'extra_bed'
]);
}]);
}]);
答案 1 :(得分:0)
您应该尝试以下操作:
$orders = $this->orderRepository
->search($filters, true)
->sort($sort)
->select([
'id',
'code',
'pro_periods_id',
'quantity_adult_single',
'quantity_adult_double',
'quantity_adult_triple',
'quantity_child_bed',
'quantity_child_no_bed',
'quantity_infant',
'created_at',
'created_by'
])
->withCount(['orderPassengers' => function($sub_query){
$sub_query->select([
'id',
'pro_orders_id',
'pro_rooms_id',
'title_name',
'first_name_en',
'last_name_en',
'birth_date'
])
->with(['room' => function($sub_query){
$sub_query->select([
'id',
'pro_periods_id',
'room_number',
'room_type',
'extra_bed'
]);
}]);
}])->get();
答案 2 :(得分:0)
您只能对模型的已定义关系进行withCount()
。
class Order extends Model
{
function orderPassengers()
{
return $this->hasMany('App\orderPassengers');
}
}