简体   繁体   中英

Laravel Filter Multiple WhereIn Array

I am trying to create a filter for an ecommerce store. A user can filter products by tags, each flavor tag is appended to an array: $tags_array

ie Show me all products that contain the 'kiwi' AND 'menthol' tag

dd($tags_array);

array:2 [
  0 => "kiwi"
  1 => "menthol"
]

Filter section of the query

$query = $tags_array ? $query
 ->join('product_product_tag', 'products.id', '=', 'product_product_tag.product_id')
 ->join('product_tags', 'product_tags.id', '=', 'product_product_tag.tag_id')
 ->whereIn('product_tags.value', $tags_array) : $query;

-One product HasMany tags

-One tag BelongsToMany products

product_tags

id | value 
------------------
1  | kiwi 
2  | apple
3  | menthol 
4  | strawberry 

-Pivot table: product_product_tag

product_product_tag

id | product_id | tag_id 
----------------------------
1  |    1       |    1
2  |    1       |    3
----------------------------
3  |    2       |    1
4  |    2       |    2
5  |    2       |    3
----------------------------
6  |    3       |    1
7  |    3       |    4
----------------------------
8  |    4       |    3

PROBLEM

Current query shows all products that have a kiwi OR menthol tag.

actual result: Products: 1, 2, 3, 4

expected result: Products: 1, 2

This query works correctly when only one tag is applied. How can I make this filter include multiple tags applied (product has kiwi tag AND menthol tag). A user can filter by 0,1,2,3,... tags

Is there a best practice for handling a dynamic number of tags?

edit: I am using Laravel DB Query Builder

2nd edit: attempting btl's answer

  $query = $tags_array ? $query
      ->join('product_product_tag', 'products.id', '=', 'product_product_tag.product_id')
      ->join('product_tags', 'product_tags.id', '=', 'product_product_tag.tag_id') : $query;
  if($tags_array) {
      foreach ($tags_array as $tag) {
          $query->where('product_tags.value', '=', $tag);
      }
  }

3rd edit : Pivoted back to the following:

$query = $tags_array ? $query
 ->join('product_product_tag', 'products.id', '=', 'product_product_tag.product_id')
 ->join('product_tags', 'product_tags.id', '=', 'product_product_tag.tag_id')
 ->where('product_tags.value', $tags_array) : $query;

This is incorrect still. Current filter returns results for the first tag found. Will keep working towards a more dynamic solution

whereIn('product_tags.value', $tags_array) will match everything that has at least one of the tags in the array, hence why it's behaving as OR . If you need to match only products with all the selected tags, you need to set up multiple where clauses, something to the effect of:

$query = Product::with('tags');
foreach ($tags_array as $tag) {
     $query->whereHas('tags', function($q) use ($tag) {
         $q->where('id', $tag);
     });
 }
 $query->get();

Edit

$query = $tags_array ? $query
 ->join('product_product_tag', 'products.id', '=', 'product_product_tag.product_id')
 ->join('product_tags', 'product_tags.id', '=', 'product_product_tag.tag_id');

foreach ($tags_array as $tag) {
    $query->where('product_tags.id', '=', $tag);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM