2017-10-04 75 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

我没有使用雄辩的先生。请用我的问题回答我 –