我试图从两个或多个查询的查询结果中获得一个组合数组。下面给出了我到目前为止尝试过的解决方案。
创建了一个变量List<T>
,并在$total_records
循环中对其进行了迭代,以获取第二组数据输出。现在,我想将输出#2合并到foreach
有一个查询#1:
total_records
输出#1:
$total_records = RatioStatistics::where('type', '=', 'group')
->whereBetween('date', [$modified_start_date, $modified_end_date])
->join('cart_abondon_group', 'statistics.ref_id', '=', 'cart_abondon_group.id')
->select('statistics.*', 'cart_abondon_group.name')
->orderBy('statistics.ref_id', 'ASC')
->orderBy('statistics.date', 'ASC')
->get();
还有另一个查询2:
[{
"id": 324985,
"ref_id": 1,
"date": "2019-01-02T00:00:13+00:00",
"total": 434,
"available": 164,
"rented": 270,
"unusable": 0
},
{
"id": 325602,
"ref_id": 1,
"date": "2019-01-03T00:00:16+00:00",
"total": 427,
"available": 176,
"rented": 251,
"unusable": 0
}]
输出2:
foreach ($total_records as $record) {
$data = [
'total_number_of_cart_abondon_of_the_day' => Cart::where('created_at', 'like', $record['date']->format('Y-m-d') . '%')
->whereNotIn('status_id', [11])
->whereHas('product', function ($q) use ($record) {
$q->where('group_id', $record['ref_id']);
})
->count(),
'total_number_of_cart_abondon_per_day_per_warehouse_user' => Cart::where('created_at', 'like', $record['date']->format('Y-m-d') . '%')
->where('warehouse_user', 1)
->whereNotIn('status_id', [11])
->whereHas('product', function ($q) use ($record) {
$q->where('group_id', $record['ref_id']);
})
->count()
];
$new_stats[] = $data;
}
请求的输出:
所需的正确输出:
"0": {
"total_number_of_cart_abondon_of_the_day": 27,
"total_number_of_cart_abondon_per_day_per_warehouse_user": 0
},
"1": {
"total_number_of_cart_abondon_of_the_day": 30,
"total_number_of_cart_abondon_per_day_per_warehouse_user": 0
}
答案 0 :(得分:1)
致电时
foreach ($total_records as $record) {
$data = [
'total_number_of_cart_abondon_of_the_day' => Cart::where...,
'total_number_of_cart_abondon_per_day_per_warehouse_user' => Cart::where...
]
}
您每条记录要进行另外的 2条sql查询。
假设RadioStatistics
是statistics
表的模型,而Cart
是cart_abondon_group
表的模型,则可以按以下方式进行固定:
RadioStatistics
模型中建立关联方法# statistics has a foreign key (ref_id) that references cart_abondon_group's primary key (id)
public function carts()
{
return this->belongsTo(Cart::class, 'ref_id', 'id'); // Assuming Cart is cart_abondon_group's model
}
$total_records = RatioStatistics::where('type', '=', 'group')
->whereBetween('date', [$modified_start_date, $modified_end_date])
->join('cart_abondon_group', 'statistics.ref_id', '=', 'cart_abondon_group.id')
// WithCount must be before select or it won't be added to the results
->withCount([
'carts as total_number_of_cart_abondon_of_the_day' => function ($carts) {
$carts->whereRaw("cart_abondon_group.created_at like concat(date_format('statistics.date', '%d-%m-%Y'), '%')")
->whereNotIn('status_id', [11])
->whereHas('product', function ($q) use ($record) {
$q->whereRaw('group_id = statistics.id');
});
},
'carts as total_number_of_cart_abondon_per_day_per_warehouse_user' => function ($carts) {
$carts->whereRaw("cart_abondon_group.created_at like concat(date_format('statistics.date', '%d-%m-%Y'), '%')")
->where('warehouse_user', 1)
->whereNotIn('status_id', [11])
->whereHas('product', function ($q) use ($record) {
$q->whereRaw('group_id = statistics.id');
});
}
])
->select('statistics.*', 'cart_abondon_group.name')
->orderBy('statistics.ref_id', 'ASC')
->orderBy('statistics.date', 'ASC')
->get();
棘手的部分是日期。我真的不明白为什么您的架构具有两种不同的格式。