社區面臨的挑戰!我有一張介紹多家商店的地圖。當用戶點擊商店時,它應該只加載相關產品。要做到這一點,我有四個表(見下文)。使用數據透視表的高級雄辯過濾
我能夠從品牌調出相關產品,如果用戶通過使用if(!is_null($gender)) $q->where($gender, '=', 1);
。然而應用了性別過濾$gender
,我希望能夠呈現即使用戶沒有正確的產品不是過濾器。
挑戰在於,一個品牌可以爲男性和女性都有產品,但放置在不同的商店(或在同一家商店)。爲了跟蹤這個,我有一個名爲brands_stores的數據透視表。
總之 - 我想補充的是像
$q->with(['products' => function ($q) use ($product_ids [GENDER(S) FROM PIVOT TABLE]){
$q->whereIn('gender', [GENDER(S) FROM PIVOT TABLE];
}
下面的現有查詢。
查詢
// Get stores
$query = Store::with(['brandsUnfiltered' => function ($q) use ($active, $gender, $product_ids){
if(!is_null($active)) $q->where('active', '=', 1); // Active stores
if(!is_null($gender)) $q->where($gender, '=', 1); // Gender
$q->with(['products' => function ($q) use ($product_ids){
$q->whereIn('id', $product_ids);
$q->orderBy('brand', 'asc')->orderBy('gender', 'desc')->orderBy('category', 'asc')->orderBy('sub_category', 'asc');
$q->groupBy('brand', 'name');
$q->select('id AS product_id', 'name', 'brand', 'price', 'img_link');
}]);
}])
->whereIn('id', $store_ids)
->select('id', 'name', 'lat', 'lng', 'formatted_address AS address', 'street_number', 'route', 'open_monday', 'open_saturday', 'open_sunday', 'close_monday', 'close_saturday', 'close_sunday', 'formatted_phone_number AS phone');
$stores = $query->get();
}
Store模式
class Store extends Eloquent {
public function brandsUnfiltered(){
return $this->belongsToMany('\App\Brand', 'brands_stores', 'store_id', 'brand_id')
->withPivot('active', 'brand_store', 'women', 'men');
}
}
商店表
- ID
- 名
- ...
品牌表
- ID
- 名
- ...
Brands_stores表
- ID
- STORE_ID
- brand_id
- 女性[布爾]
- 男人[布爾]
- ...
Products表
- ID
- 名
- 品牌
- 性別
- ...
=====編輯=====
產品掛鉤於BRANDS與之連接的STORES
每個產品都有一個品牌欄目。該品牌列與品牌模型中的名稱列相同。 Brands_Stores TABLE將哪些品牌以及該商店的哪些商品通過該品牌鏈接起來。邏輯流程:
- 該店是否攜帶相關品牌?
- 如果它攜帶相關品牌 - (男性或女性或兩者),其產品
- 獲取有關產品
SQL查詢
select `brand` from `products` where `name` like '%louboutin%' or `brand` like '%louboutin%' group by `brand`, `name`
select * from `products` where (`brand` in ('Christian Louboutin')) group by `brand`, `name`
select `id` from `brands` where `name` in ('Christian Louboutin')
select `store_id` from `brands_stores` where (`active` = '1' and `brand_id` in ('278'))
select `id`, `name`, `formatted_phone_number` as `phone` from `stores` where `id` in ('561', '562', '563', '2182')
select `brands`.*, `brands_stores`.`store_id` as `pivot_store_id`, `brands_stores`.`brand_id` as `pivot_brand_id`, `brands_stores`.`active` as `pivot_active`, `brands_stores`.`brand_store` as `pivot_brand_store`, `brands_stores`.`women` as `pivot_women`, `brands_stores`.`men` as `pivot_men`, `brands_stores`.`children` as `pivot_children` from `brands` inner join `brands_stores` on `brands`.`id` = `brands_stores`.`brand_id` where `brands_stores`.`store_id` in ('562', '2182') and `active` = '1'
select `id` as `product_id`, `name`, `brand`, `price`, `img_link` from `products` where `products`.`brand` in ('Christian Louboutin') and `id` in ('6800', '7538', '7612', '7582', '8095', '7104', '8053', '7995', '7115', '7485', '7997', '7866', '7622', '6820', '7682', '8000', '8055', '6838', '7589', '7046', '7232', '6810', '7609', '7429', '7597', '7557', '7593', '7458', '7481', '7572', '7620', '7238', '7537', '6843', '7619', '7598', '8036', '7284', '6956', '7993', '6863', '8039', '7614', '7493', '7315', '7318', '6841', '7509', '7198', '5813', '8203', '7623', '7441', '8096', '7957', '6522', '6850', '8056', '7821', '6753', '6632', '7569', '7994', '7784', '9388', '9431', '9440', '9392', '6348', '6373', '5989', '7339', '7329', '7340', '7502', '7544', '7586', '7636', '7252', '7179', '7564', '6771', '6461', '6554', '6563', '6442', '5756', '5770', '7439', '7373', '7313', '7349', '7293', '7595', '7323', '7565', '8073', '9363', '9411', '5910', '5899', '6136', '5828', '6577', '6601', '7551', '7392', '7543', '7872', '8076', '7517', '7849', '7531', '7602', '7410', '6836', '7401', '6851', '8034') group by `brand`, `name` order by `brand` asc, `gender` desc, `category` asc, `sub_category` asc
一)如何做你的'product'表中的數據模型適合?我不能看到連接它的'product_id' b)請提供MySQL查詢,而不是它的創建語法。 – Benvorth
@Benvorth請參閱更新問題 –