D
D
Daniil Kondratovich2015-06-21 20:33:20
MySQL
Daniil Kondratovich, 2015-06-21 20:33:20

php product filter on laravel?

Good day to all.
The task was to write a filter by parameters for a site based on laravel.
Made the following implementation:
Tables: products, params, values ​​and product_value.
The params table is associated with values ​​and values ​​is associated with products.
Each parameter has multiple values ​​and each product has multiple values.
The relationship is implemented using the eloquent hasMany and belongsToMany functions.
The selection is performed as follows:
In the parameter block, using foreach, the values ​​for each parameter are displayed. The user selects the parameters, this data is sent to the server as an array.
This array is then traversed using foreach. For each iteration, there is a selection of records from the database that have the value of the parameter written to the current element of the array.
Thus, we get another, already two-dimensional, array with arrays of identifiers of suitable products. Then, using the array convergence function, products are selected that are suitable in all respects. They are subsequently displayed to the user.
The question is as follows. Is there a lighter implementation in terms of server load, and is there a simpler way to do this? Although my version seems to be the most universal, but, something tells me, it is not rational.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Galkin, 2015-06-21
@ktulxu

If I understand correctly, you get an array of values ​​from the values ​​table from the frontend.
Option 1: not to get the products associated with them through Has Many Through , and then not to cross the collections?
Option 2: make a Scope that, using the values ​​array, gets all the products that have such values.
Maybe I don't understand correctly and we get upper and lower values ​​for each of the params from the frontend - then only option 2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question