有没有一种方法可以合并或合并从Laravel查询接收到的两个数组?

时间:2019-10-24 11:26:32

标签: php laravel

我试图从两个或多个查询的查询结果中获得一个组合数组。下面给出了我到目前为止尝试过的解决方案。

创建了一个变量List<T>,并在$total_records循环中对其进行了迭代,以获取第二组数据输出。现在,我想将输出#2合并到foreach

的第一个位置接收的输出#1中

有一个查询#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;
}

请求的输出:

  1. 要么我要么要做一个这样的解决方案,以便我们可以通过第一个查询本身查询所有内容,而无需运行第二个查询。
  2. 或者进一步优化第二个查询,以便将输出#2合并为输出#1

所需的正确输出:

"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
}

1 个答案:

答案 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查询。

假设RadioStatisticsstatistics表的模型,而Cartcart_abondon_group表的模型,则可以按以下方式进行固定:

  1. 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
}
  1. 将查询1重新制作为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')
// 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();

棘手的部分是日期。我真的不明白为什么您的架构具有两种不同的格式。