I don't have the link, but search and @martinbean and @tray2 has has some good post on product variations similar to what you ask.
May 29, 2024
5
Level 1
how to get data from a double relationships?
I have tables of categories, products and colors. I apply a many-to-many relationship. I need to get all categories that have black colored products.
To get categories that have products I do this:
$categories = Category::has('products')->get();
How can I expand my query to add a search based on color? I try this, but nothing works.
$categories = Category::has('products')->whereHas('colors', function ($query){
$query->where('title', 'black');
})->get();
Level 75
This should work:
$categories = Category::whereHas('products.colors', function ($query) {
$query->where('title', 'black');
})->get();
1 like
Please or to participate in this conversation.