Reputation: 1859
I have a query from my database where i want to make the result into array, I am using laravel but i am not using Eloquent because i am just a beginner. Here is my query.
Query:
$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);
Here my query result is this:
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"
}
]
}
In the result that i've got, i am not satisfied because i want a result like this:
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"
}
}
how can i make it to this? please help me build with the correct query. I want to make the products as sub array
Here is my table structure:
Shipping table:
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)
Upvotes: 1
Views: 3049
Reputation: 1713
you can use foreach() for this type of result here is some code for help
$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();
}
here its response link hope it will help you
Upvotes: 1
Reputation: 241
the answer that you cant do it in 1 query. you can do it in two queries. but the best answer for you is to start using Eloqent and then it is super simple
$shipping = Shipping::with('productShipping')->get();
mySQL always return a single table in one query you cant make complex objects in one query
Upvotes: 1
Reputation: 771
You can use is this way:
$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();
Upvotes: 0