我有一个来自我的数据库的查询,我想将结果变成数组,我正在使用laravel但我不使用Eloquent因为我只是一个初学者。这是我的询问。
查询:
mistakes
我的查询结果如下:
helpArrayFunc()
在我得到的结果中,我不满意因为我想得到这样的结果:
$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"
}
]
}
Shipping_products:
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"
}
}
Products_table:
Ship_ID (INT AUTO INC)
AMOUNT (DOUBLE,2)
NAME (VARCHAR)
SHIP_DATE (DATE)
RECEIVER (VARCHAR)
答案 0 :(得分:1)
你不能在1个查询中做到的答案。 你可以在两个查询中完成。 但对你来说最好的答案是开始使用Eloqent然后它是超级简单的
$shipping = Shipping::with('productShipping')->get();
mySQL总是在一个查询中返回一个表,您无法在一个查询中创建复杂对象
答案 1 :(得分:1)
您可以将foreach()用于此类结果 这里有一些帮助代码
$package_data = DB::table('packages_type')->where('flag','0')->select('id','name')->get();
foreach ($package_data as $key => $value) {
$package_data[$key]->package_det = DB::table('plans')->where('p_type_id',$value->id)
->leftJoin('packages_time', 'plans.p_time_id', '=', 'packages_time.id')
->select('plans.amount','packages_time.name as time_name')
->get();
}
此处回复link 希望它会帮助你
答案 2 :(得分:0)
你可以这样使用:
$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()->all();