我有这个功能:
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
查询中绑定这两个?
到目前为止,我可以使用cat
的whereIn
但是你看到$subs
和$prod
都试图获得DB::table('products')
,这就是我猜的问题。
我得到的这个错误:
SQLSTATE [HY093]:无效的参数编号:混合的命名和位置参数(SQL:select * from
products
,其中products
.category_id
in(3,4)group by`[{“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
,这是我保存的y数据的正确答案。
我混合了我的$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();