E
E
Evgeny Romashkan2018-04-26 18:07:26
Laravel
Evgeny Romashkan, 2018-04-26 18:07:26

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 from a specific category:
$subcategories = Subcategory::where('category_id','=', $category_id)->get();

Question - how to select all products from a certain category? Educational project.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2018-04-26
@EvgeniiR

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 question

Ask a Question

731 491 924 answers to any question