[英]Laravel withcount
I need to count orderpassenger with withcount我需要用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');
This my result enter image description here这是我的结果在此处输入图像描述
I get nothing with withcount.我一无所获。
How can I get orderPassenger_count.我如何获得 orderPassenger_count。
Try this:尝试这个:
$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'
]);
}]);
}]);
You should try this:你应该试试这个:
$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();
You can only do a withCount()
on a defined relation of the model.您只能对模型的定义关系执行
withCount()
。
class Order extends Model
{
function orderPassengers()
{
return $this->hasMany('App\orderPassengers');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.