Reputation: 3202
I have an product filter option .and user has product filters of different type in product category page.
Tags
id | tag_name | slug |
products
id | proudct_name
product_tags
id | product_id | tag_id
Now am trying to retrieve all products where tag_name ="ddd" and tag_name="ddd".
It means product should be in both the category .
$response=Product::with(['productCategory'])
->when((isset($requestArray)&&count($requestArray)),function ($query)use($requestArray){
$query->whereHas('productTags', function ($query) use ($requestArray) {
$query->where(function ($query) use ($requestArray){
if (isset($requestArray['filterForm'])&&count($requestArray['filterForm'])){
$valueArray=[];
foreach ($requestArray['filterForm'] as $key=>$value){
if (is_array($value)&&count($value)){
$query->whereIn('tags.slug',$value);
} elseif($key!="country_list"){
$query->where('tags.slug',$value);
}
}
}
});
});
})
->whereHas('productCategory',function ($query)use($request,$categoryName){
$query->where('slug',$categoryName);
});
I have issue when i select combination of mobile brand and mobile color/if i select only one filter group then it works fine
Upvotes: 2
Views: 201
Reputation: 13394
The color and mobile_brand are all in tags.slug
,
The slug cannot have color's value and mobile_brand's at the same time.
Do something like this:
// You need to build the combination of mobile_band and color to array:
$slugs = array(['green', 'mi'], ['green', 'lg']);
$response = Product::with(['productCategory'])->where(function($query) use ($slugs) {
forEach ($slugs as $tags)
{
$query->orWhere(function($query) use ($tags) {
forEach($tags as $tag)
{
$query->whereHas('productTags', function($query) use ($tag){
$query->where('tags.slug', $tag);
});
}
});
}
});
Upvotes: 2