0

Is there a way to add a where clause on one of the tables of the pivot table inside the pivot table ? For example in the code below, how can i add a where clause in products_categories to specify the products with type='ELECTRONICS' ?

DB::table("products_categories")
    ->whereIn("category_id", array_values($ids))
    ->pluck("id")
    ->all();
Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
Abdo Rabah
  • 1,670
  • 2
  • 15
  • 31
  • 1
    This post could help you https://stackoverflow.com/questions/18538527/how-to-filter-a-pivot-table-using-eloquent – Alex197 Dec 20 '21 at 09:29

1 Answers1

1

Try this:

$type = "ELECTRONICS";
$products_categories = Product::with('categories')->whereHas('categories', function($q) use ($type){
    $q->where('type', "%$type%");
})->get();