laravel嵌套的foreach循环生成重复

时间:2017-03-18 16:56:30

标签: php laravel laravel-5.2

此方法返回json响应。每个订单都有很多产品,我希望数据阵列中的产品密钥具有与该订单相关的产品名称。

$customerData = Order::whereAdminId($this->admin->id)->whereCustomerId($customer->id)->
whereBetween('created_at', array($date_from,$date_to))->get();

if (count($customerData) > 0) {
    $data1 = array();
    $data3 = array();
    foreach ($customerData as $value) {
        foreach ($value->orderProducts as $value1) {            
            $data3[] = array($value1->products($value1->product_id)->name);
        }
        $data[] = array(
            'product'=>$data3, 
        );
    }
    return response()->json(['success'=>true, 'customerData'=>$data], 200);
}

但是在嵌套的foreach之前的值添加到新值,并且每个订单的所有产品名称都是重复的。感谢

0 个答案:

没有答案