【问题标题】:Laravel 5 using raw query getting result to arrayLaravel 5 使用原始查询获取结果到数组
【发布时间】:2018-03-15 11:01:01
【问题描述】:

我有一个来自我的数据库的查询,我想将结果放入数组中,我正在使用 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

Products_table:

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

【问题讨论】:

  • 你需要不止一个查询
  • @GauravGupta 感谢您的回复先生。你能给我一些链接作为示例吗?
  • 需要你的表格结构
  • @GauravGupta 好的先生,我会用它发布一个汇总结构表
  • @JcJohn 尝试在->get() 后面添加toArray()

标签: php mysql laravel left-join laravel-5.4


【解决方案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 希望对你有帮助

【讨论】:

  • 您好先生,您能帮我吗?我收到错误解析错误:语法错误,意外'(',期望标识符(T_STRING)或变量(T_VARIABLE)或'{'或'$'
  • 这里是代码:$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') ->where(['shipping_products.shipping_id', '=', $key ])->get(); } dd($shippings);
  • 你忘记了在这个附近的 foreach 循环检查中加入一个 ->‌​('products.featured_‌​img','products.produ‌​ct_name', 'products.price','shi
  • 随时乐意提供帮助 :)
  • 你能帮我添加->分页(8)
【解决方案2】:

您无法在 1 个查询中完成的答案。 你可以在两个查询中做到这一点。 但对你来说最好的答案是开始使用 Eloqent,然后它就超级简单了

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

mySQL 总是在一个查询中返回一个表,你不能在一个查询中创建复杂的对象

【讨论】:

  • 我没有使用雄辩的先生。请回答我的问题
【解决方案3】:

你可以这样使用:

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

【讨论】:

  • 调用未定义的方法 Illuminate\Database\Query\Builder::lists()
  • 感谢您的快速回复先生。我马上试试
  • Illuminate\Database\Query\Builder::pluck() 缺少参数 1
  • 如果您需要以数组格式获取所有用户模型属性,我只是浏览文档,然后您可以使用->get()->all(),而如果您只需要获取选定的属性,那么您可以这样做。 ->pluck($attributes)->all() pluck 方法采用逗号分隔的字符串。
  • 不好意思说!它不能解决我的问题。先生,当我尝试它时,它给了我在顶部发布的相同输出,而不是我想要在帖子底部实现的查询。请查看我的帖子
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 2016-05-06
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多