Answer the question
In order to leave comments, you need to log in
How to select all products from a certain category if they have subcategories?
An online store has a table of categories (categories),
subcategories (subcategories) with a category_id field,
and a product table with a subcategory_id field.
Each subcategory must belong to one category. Each product belongs to one category.
You can get all products from a specific subcategory like this:
$products = Product::where('subcategory_id','=', $subcategory_id)->get();
$subcategories = Subcategory::where('category_id','=', $category_id)->get();
Answer the question
In order to leave comments, you need to log in
https://laravel.com/docs/5.6/eloquent-relationships
whereHas
Naturally, before that, you need to register some kind of relationship between the models.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question