2017-10-04 38 views
0

我有一個查詢從我的數據庫,我想使結果成數組,我使用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) 
+0

您需要多一個查詢 –

+0

@GauravGupta感謝您的回覆先生。你可以給我一些鏈接作爲樣本來做到這一點? –

+0

需要你的表格結構 –

回答

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 希望這將有助於你

+0

你好,先生你可以幫我嗎?出現錯誤解析錯誤:語法錯誤,意外的'(',期待標識符(T_STRING)或變量(T_VARIABLE)或'{'或'$' –

+0

以下是代碼:$ shippings = DB :: table('shipping_table') - >選擇( '*') - >其中([ 'shipping_table.shipping_status'=> $未決]) - >得到(); 的foreach($運送爲$密鑰=> $值){ \t $ ('shipping_products') - >('products.featured_img','products.product_name', \t \t \t'products.price','shipping_products.quantity') \t \t \t - > join('products','shipping_products.product_id','=','products.product_id') \t \t \t - >其中([ 'shipping_products.shipping_id', '=',$鍵]) \t \t \t - >得到(); } dd($ shippings); –

+0

你忘了一個加入foreach循環檢查附近 - >('products.featured_img','products.product_t_name','products.price','shi –

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(); 
+0

調用未定義的方法Illuminate \ Database \ Query \ Builder :: lists() –

+0

感謝您的快速回復先生。我馬上試試 –

+0

更新了答案 謝謝@Badea Mihai Florin –

1

你不能在1查詢中做的答案。 你可以在兩個查詢中做到這一點。 但你最好的答案是,開始使用Eloqent,然後它是超級簡單

$shipping = Shipping::with('productShipping')->get(); 

的MySQL總是返回一個查詢一個表,你不能在一個查詢使複雜的對象

+0

我沒有使用雄辯的先生。請用我的問題回答我 –