Answer the question
In order to leave comments, you need to log in
How to get products from a category?
I have three tables: product - products, catalog - product categories, product_catalog - table combining products and categories.
Tell me, how can I get products in the category I have chosen on laravel?
Answer the question
In order to leave comments, you need to log in
In product, records have a field, what category do they belong to?
If not, then do it.
Let's use relationships. Add a method to the Category model:
public function products()
{
return $this->belongsToMany(Prdouct::class, 'product_catalog', 'category_id', 'product_id', 'id');
}
Categoty::find(1)->products;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question