Answer the question
In order to leave comments, you need to log in
Query level sorting or sortBy on laravel collection?
Goodnight! In my database, product data is stored using EAV - although indirectly, this is related to the problem.
The bottom line is that the product has different characteristics, and the characteristics have corresponding values. I need to sort the selection by a certain field in a table with a characteristic value, but at the same time relying on a specific field in a table with characteristics. I do not know the number of characteristics themselves, the user can create an infinite number of them.
All my connections are implemented by the standard features of eloquent orm. The data array looks like this
$products = [
[
'id' => 1,
'title' => 'Product N',
'specifications' => [
[
'id' => 2,
'title' => 'Display',
'position' => 1,
'value' => [
'id' => 22,
'value' => 'OLED',
'position' => 1
]
],
[
'id' => 3,
'title' => 'Storage',
'position' => 3,
'value' => [
'id' => 11,
'value' => '16GB',
'position' => 1
]
],
]
],
[
'id' => 2,
'title' => 'Product C',
'specifications' => [
[
'id' => 2,
'title' => 'Display',
'position' => 1,
'value' => [
'id' => 22,
'value' => 'IPS',
'position' => 3
]
],
[
'id' => 3,
'title' => 'Storage',
'position' => 3,
'value' => [
'id' => 12,
'value' => '32GB',
'position' => 2
]
],
]
]
];
Answer the question
In order to leave comments, you need to log in
Join all the characteristics one by one and sort as usual via orderBy It is
pointless to sort the collection, the output will be sorted on each page of a separate result, after all.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question