samedi 23 mars 2019

Eloquent Query to find closest match based on relationship parameters

I have a situation where I have 4 user inputs.

Strength, bitterness, tartness, sweetness.

My relevant database setup is: Table: Food Food->Hasmany->Food Parameters (one of each of the above to match with inputs) it's setup this was so that parameters can be dynamic without altering the Food table...

I need to design a query such that it orders results displayed to the user from closest to farthest. For the the sake of this let's say everything has an equal weight.

I know that I could (sudo code):

foreach(user_input)
     total = total + absolut_value(user_input - matching_food_param)

and then order all the totals from smallest to largest.. I don't have an idea how to even begin to write that in Laravel.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire