Answer the question
In order to leave comments, you need to log in
SortBy or OrderBy on Laravel relationship collection?
Good evening. There is a task of sorting of values, at selection from basis. In general, if I implement a one-to-one relationship, there are no problems, everything works. But I have an EAV template in my database. Each product belongs to a group of characteristics. Each characteristic has a position field, according to which I set the sorting priority. After that, each feature has a link to a value associated with the product. And these values also have a position field.
Let's say I got the data from the database and do the sorting
$products->sortBy(function($q){
return $q->relatedProduct->relatedSpecsGroup->position;
})
[
['id', 'title','position','relatedValue'=>['id','value','position']],
['id', 'title','position','relatedValue'=>['id','value','position']],
['id', 'title','position','relatedValue'=>['id','value','position']],
['id', 'title','position','relatedValue'=>['id','value','position']],
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question