通过join将对象返回到对象中的数组

时间:2017-04-12 18:07:03

标签: php mysql sql frameworks laravel-5.2

我有2张桌子: 的订单(ORDER_ID,order_code,USER_ID,FULL_NAME,电话,地址,recieve_address,total_product,TOTAL_PRICE,状态,created_at,的updated_at)

detail_order (ID,ORDER_ID,order_code,PRODUCT_ID,价格,数量,TOTAL_AMOUNT,created_at,的updated_at)

查询获取数据:

$id=Auth::user()->id;

$result=DB::table('orders')->join('detail_orders', function ($join) use($id) {
    $join->on('orders.order_id', '=', 'detail_orders.order_id')
    ->where('orders.user_id', '=', $id);
})->get();

dd($ result)时的结果:

array:2 [▼
  0 => {#194 ▼
    +"order_id": 37
    +"order_code": "Order00003"
    +"user_id": 1
    +"full_name": "Duc.VUong"
    +"phone": "01203388870"
    +"address": "445/33b bình đông"
    +"recieve_address": "445/33b bình đông"
    +"total_product": 2
    +"total_price": 1450000.0
    +"status": 0
    +"created_at": "2017-04-12 17:01:38"
    +"updated_at": "2017-04-12 17:01:38"
    +"id": 29
    +"product_id": 1
    +"price": 800000.0
    +"quantity": 1
    +"total_amount": 800000.0
  }
  1 => {#187 ▼
    +"order_id": 37
    +"order_code": "Order00003"
    +"user_id": 1
    +"full_name": "Duc.VUong"
    +"phone": "01203388870"
    +"address": "445/33b bình đông"
    +"recieve_address": "445/33b bình đông"
    +"total_product": 2
    +"total_price": 1450000.0
    +"status": 0
    +"created_at": "2017-04-12 17:01:38"
    +"updated_at": "2017-04-12 17:01:38"
    +"id": 30
    +"product_id": 2
    +"price": 650000.0
    +"quantity": 1
    +"total_amount": 650000.0
  }
]

如果我想结果如下:

array:1 [▼
  0 => {#194 ▼
    +"order_id": 37
    +"order_code": "Order00003"
    +"user_id": 1
    +"full_name": "Duc.VUong"
    +"phone": "01203388870"
    +"address": "445/33b bình đông"
    +"recieve_address": "445/33b bình đông"
    +"total_product": 2
    +"total_price": 1450000.0
    +"status": 0
    +"created_at": "2017-04-12 17:01:38"
    +"updated_at": "2017-04-12 17:01:38"
    +"detail":[
    0=>{
        +"product_id": 1
        +"price": 800000.0
        +"quantity": 1
        +"total_amount": 800000.0
    }
    1=>{
        +"product_id": 2
        +"price": 650000.0
        +"quantity": 1
        +"total_amount": 650000.0
    }
    ] 
  }
]

数组对象的细节将通过order_id获得。那我怎么能通过查询呢?

0 个答案:

没有答案