A
A
Andrey Boychenko2019-06-07 00:31:44
Laravel
Andrey Boychenko, 2019-06-07 00:31:44

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
                        ]
                    ],
                ]
               ]
        ];

You need to sort as follows, go to specifications, take the element with the smallest position value, go to value. Sort by position. We take the next element of the specifications array and around. Until we sort all the values.
Moreover, products can have different groups of characteristics, respectively, you need a universal solution. Please tell me how to do this kind of sorting in general. Is it possible to do all this at the request level?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2019-06-07
@Sanasol

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 question

Ask a Question

731 491 924 answers to any question