'Laravel WhereIn & With Query merge

so I’m running this query when a user selects multiple categories on the frontend of the site, I want it to bring back the products that are related to the selected categories… this works, however, I have 2 collections that I need to merge together. Here is the code:

$category = Category::whereIn('permalink', $request->get('categoryFilter'))->with('products')->get()->pluck('products');

I get the following back:

Illuminate\Support\Collection {#1779 ▼
  #items: array:2 [▼
    0 => Illuminate\Database\Eloquent\Collection {#1780 ▼
      #items: []
      #escapeWhenCastingToString: false
    }
    1 => Illuminate\Database\Eloquent\Collection {#1799 ▼
      #items: array:24 [▶]
      #escapeWhenCastingToString: false
    }
  ]
  #escapeWhenCastingToString: false
}

The first category has no products but the second has, is there a way of merging these 2 together?



Solution 1:[1]

Try this,

$category = Products::whereIn('category_id',Category::whereIn('permalink', 
$request->get('categoryFilter')
)->pluck('id')
    )->get();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tipu Sultan Eiko