Answer the question
In order to leave comments, you need to log in
Many-to-many relationship with join in Laravel?
There are 3 tables: products, product_parameter, parameters.
In products, there are 2 products that have certain parameters that are listed in the parameters table. The product_parameter table is the link table.
The task is to filter products by parameters. No matter how hard I tried, extra copies of products were always displayed. I decided once again to start from scratch, I wrote the following code.
$goods = DB::table('products')
->join('product_parameter as product_parameter', 'products.id', '=', 'product_parameter.product_id')
->join('parameters as parameters', 'product_parameter.parameter_id', '=', 'parameters.id')
->select('products.*')->get();
Answer the question
In order to leave comments, you need to log in
Use ORM, not DB::table, no need to suffer. And so the result is correct, for each match you have an extra record and then you need to group by a unique field. Maybe it will work - groupBy('products.id')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question