Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5使用原始查询将结果获取到数组_Php_Mysql_Laravel_Left Join_Laravel 5.4 - Fatal编程技术网

Php Laravel 5使用原始查询将结果获取到数组

Php Laravel 5使用原始查询将结果获取到数组,php,mysql,laravel,left-join,laravel-5.4,Php,Mysql,Laravel,Left Join,Laravel 5.4,我有一个来自我的数据库的查询,我想把结果变成数组,我使用的是laravel,但我没有使用Eloquent,因为我只是一个初学者。这是我的问题 查询: $array = array(); $shippings = DB::table('shipping_table') ->select('*') ->leftJoin('shipping_products', function ($join) use ($array) {

我有一个来自我的数据库的查询,我想把结果变成数组,我使用的是laravel,但我没有使用Eloquent,因为我只是一个初学者。这是我的问题

查询:

 $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)
运输产品:

ID (INT AUTO INC)
Ship_id (foreign key from shipping table)
Product_id
产品列表:

Product_id (Auto Inc)
name(varchar)
Qty(int)
Description (varchar)

您可以这样使用:

   $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();

答案是你不能在一个查询中完成。 您可以在两个查询中完成。 但对你来说最好的答案是开始使用Eloqent,然后它就非常简单了

$shipping = Shipping::with('productShipping')->get();
mySQL总是在一个查询中返回一个表您不能在一个查询中生成复杂的对象

您可以使用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();
 }
这里是它的回应
希望它能帮助您

您需要一个以上的查询that@GauravGupta谢谢你的答复,先生。你能给我一些链接作为例子吗?需要你的表结构吗that@GauravGupta好的,先生,我会用it@JcJohn尝试在
->get()
之后的末尾添加
toArray()
。调用undefined方法illumb\Database\Query\Builder::lists()。谢谢您的快速回复,先生。我将立即尝试使用Illumb\Database\Query\Builder::Pulk()的参数1。我只需浏览文档,如果需要以数组格式获取所有用户模型的属性,则可以使用
->get()->all()
,而如果只需要获取选定的属性,则可以这样做
->pulk($attributes)->all()
pulk方法使用逗号分隔的string.sry来表示!。这不能解决我的问题。当我尝试它时,先生,它给我的输出与我在顶部发布的相同,而不是我想在我的文章底部实现的查询。请检查我的帖子,我没有使用雄辩的先生。请回答我的问题你好,先生,你能帮我吗?我得到了错误解析错误:语法错误,意外的“(”,期望标识符(T_字符串)或变量(T_变量)或“{”或“$”这是代码:$shippings=DB::table('shipping_table')->select('*')->where(['shipping_table.shipping_status'=>$pending])->get();foreach($shippings as$key=>$value){$shippings[$key]->products=DB::table('shipping_products')->('products.featured_img'、'products.product_name'、'products.price'、'shipping_products.quantity')->join('products'、'shipping_products.product_id'、'='、'products.product_id')->其中('shipping_products.shipping_id'、'=','key])->get()}dd($shippings);您忘记了在该->‌​(‘产品’特色_‌​img’,“products.produ”‌​你能帮我添加->分页吗