【问题标题】:join two query together in laravel在 laravel 中将两个查询连接在一起
【发布时间】:2018-03-05 12:28:31
【问题描述】:

我有这个功能:

    public function landings($slug){
        $landing = Landing::where('slug', $slug)->firstOrFail();

    //use to get categories id stored in DB
        $cat = DB::table('landing_categories')->where('landing_id', $landing->id)->get();
        foreach ($cat as $key) {
          $id[] = $key->category_id;
        }

// use to get specifications id and compare it with products that have same specifications id's
        $subs = DB::table('landing_specifications')->where('landing_id', $landing->id)
        ->join('product_subspecification', function ($keys) {
            $keys->on('product_subspecification.subspecification_id', '=', 'landing_specifications.subspecification_id');
        })
        ->join('products', 'products.id', '=', 'product_subspecification.product_id')
        ->get();
    // dd($subs);

// using this to get result of 2 queries above at once
        $prod = DB::table('products')
        ->whereIn('products.category_id', $id)
        ->groupby($subs)
        ->get();
        return view('front.landing', compact('landing', 'prod'));
      }

查询:$cat & $subs 正在按预期工作并返回我正在寻找的结果。

我的问题是如何在$prod 查询中绑定这两个?

到目前为止,我可以通过whereIn 使用cat,但正如你所看到的$subs$prod 都试图获得DB::table('products'),这就是我猜的问题。

我得到这个错误:

SQLSTATE[HY093]:无效参数号:混合命名和位置 参数 (SQL: select * from products where products.category_id 在 (3, 4) 分组中 `[{"id":1,"landing_id":8,"s // 待续...

PS: 如果我使用 ->union($subs) 而不是 ->groupby($subs) 我会得到:

getBindings 方法不存在。

更新

dd($id);

array:2 [▼
  0 => 3
  1 => 4
]

dd($subs);

Collection {#721 ▼
  #items: array:5 [▼
    0 => {#710 ▼
      +"id": 1
      +"landing_id": 8
      +"subspecification_id": 1
      +"product_id": 1
      +"title": "product one"
      +"slug": "product-one"
      +"imageOne": "product-1517990897.jpg"
      +"imageTwo": "product-1517990897.png"
      +"short_description": "<p>this is first testing product</p>"
      +"description": "<p>this is first testing product this is first testing product this is first testing product this is first testing product this is first testing product this is ▶"
      +"price": "10000000"
      +"meta_description": null
      +"meta_tags": null
      +"arrivalDays": "2"
      +"height": "100"
      +"weight": "1"
      +"lenght": "100"
      +"width": "100"
      +"sku": "1985629"
      +"stock": "16"
      +"status_id": 1
      +"brand_id": 2
      +"category_id": 1
      +"subcategory_id": 2
      +"created_at": "2018-02-07 15:05:00"
      +"updated_at": "2018-02-28 14:16:46"
    }
    1 => {#716 ▶}
    2 => {#714 ▶}
    3 => {#720 ▶}
    4 => {#718 ▶}
  ]
}

PS:当我将$cat$subs 放在一起时,它应该将结果减少到1,这是我根据保存的数据得出的正确答案。

【问题讨论】:

  • 我认为,您需要将这两个查询的相似结果组合在一起,以便您可以进行联合查询
  • @SagarGautam 抱歉没听清楚,是什么意思?
  • 您能添加$cat$prod 的示例输出吗?
  • @SagarGautam 当然。
  • 上面的$cat和$prod在哪里?

标签: php laravel


【解决方案1】:

已解决

我混合了我的$subs$prod,如下所示,它工作得很好:

$prod = DB::table('landing_specifications')->where('landing_id', $landing->id)
    ->join('product_subspecification', function ($keys) {
        $keys->on('product_subspecification.subspecification_id', '=', 'landing_specifications.subspecification_id');
    })
    ->join('products', 'products.id', '=', 'product_subspecification.product_id')
    ->whereIn('products.category_id', $id)
    ->groupby('products.id')
    ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多