我有一个查询从我的数据库,我想使结果成数组,我使用laravel,但我不使用雄辩,因为我只是一个初学者。这是我的查询。Laravel 5使用原始查询获得结果到数组
查询:
$array = array();
$shippings = DB::table('shipping_table')
->select('*')
->leftJoin('shipping_products', function ($join) use ($array) {
$join->on('shipping_products.shipping_id','=','shipping_table.shipping_id');
})
->leftJoin('products', function ($join) use ($array) {
$join->on('products.product_id', '=', 'shipping_products.product_id');
})->get();
dd($shippings);
这里我的查询结果是这样的:
Collection {#296 ▼
#items: array:2 [▼
0 => {#297 ▼
+"shipping_id": 56
+"shipping_date": null
+"total_amount": 95000
+"shipping_receiver": "Robert"
+"shipping_address": "test"
+"mobile_number": "09992238185"
+"user_id": 1
+"shipping_status": 0
+"paywith": "paypal"
+"id": 9
+"product_id": 1
+"quantity": 15
+"subcategory_id": 1
+"featured_img": "sample image1.jpg"
+"product_name": "Product 1"
+"description": "Product 1 desc"
+"price": 46000.0
+"status": "published"
+"created_at": "2017-10-04 05:29:59"
+"updated_at": "2017-10-04 05:29:59"
}
1 => {#287 ▼
+"shipping_id": 56
+"shipping_date": null
+"total_amount": 95000
+"shipping_receiver": "Robert"
+"shipping_address": "Test"
+"mobile_number": "09992238185"
+"user_id": 1
+"shipping_status": 0
+"paywith": "paypal"
+"id": 10
+"product_id": 2
+"quantity": 10
+"subcategory_id": 1
+"featured_img": "DSC04477-e1490885078524-200x300.jpg"
+"product_name": "Product 2"
+"description": "Product 2 desc"
+"price": 49000.0
+"status": "published"
+"created_at": "2017-10-04 05:58:55"
+"updated_at": "2017-10-04 05:58:55"
}
]
}
在,我已经得到了结果,我并不满意,因为我想这样的结果:
Collection {#296 ▼
#items: array:1 [▼
0 => {#297 ▼
+"shipping_id": 56
+"shipping_date": null
+"total_amount": 95000
+"shipping_receiver": "test"
+"shipping_address": "tester"
+"mobile_number": "09992238185"
+"user_id": 1
+"shipping_status": 0
+"paywith": "paypal"
#products: array:2 {
0 => {
+"product_id": 1
+"quantity": 15
+"subcategory_id": 1
+"featured_img": "sample.jpg"
+"product_name": "Product 1"
+"description": "Product desc 1"
+"price": 46000.0
+"status": "published"
+"created_at": "2017-10-04 05:29:59"
+"updated_at": "2017-10-04 05:29:59"
}
1 => {
+"product_id": 2
+"quantity": 10
+"subcategory_id": 1
+"featured_img": "DSC04477-e1490885078524-200x300.jpg"
+"product_name": "Product 2"
+"description": "Product Desc 2"
+"price": 46000.0
+"status": "published"
+"created_at": "2017-10-04 05:29:59"
+"updated_at": "2017-10-04 05:29:59"
}
}
我该如何做到这一点?请帮助我用正确的查询来构建。我想使产品作为子阵列
这里是我的表结构:
航运表:
Ship_ID (INT AUTO INC)
AMOUNT (DOUBLE,2)
NAME (VARCHAR)
SHIP_DATE (DATE)
RECEIVER (VARCHAR)
Shipping_products:
ID (INT AUTO INC)
Ship_id (foreign key from shipping table)
Product_id
Products_table:
Product_id (Auto Inc)
name(varchar)
Qty(int)
Description (varchar)
您需要多一个查询 –
@GauravGupta感谢您的回复先生。你可以给我一些链接作为样本来做到这一点? –
需要你的表格结构 –