thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

Relationship result sorting #5648

Closed toto975 closed 1 year ago

toto975 commented 2 years ago

Laravel version

9.7.0

PHP version

8.1.4

Voyager version

1.5

Database

8.0.28

Description

In a select dropdown relationship, datas are not sorting as defined in the sort option of the relationship

Steps to reproduce

My Model : Parcours belongsto Cadre, with a sort on the name image

When adding a Parcour, the select dropdown loads datas 50 by 50 (I show the json results) Page 1 image Page 2 image Page 3 image

It seems that datas are sorted by id, then by name for Page 1 and 2 By name then by id on page 3.

Expected behavior

The relation datas must be sorted by 'name' first, then take/limit and skip/offset.

In voyagerBaseController->relation, some changes since this fixe https://github.com/the-control-group/voyager/pull/4858 to include sorting But sorting is done after take/limit and skip/offset.

For sample, line 928 $relationshipOptions = $model->take($on_page)->skip($skip)->get(); and line 945 $relationshipOptions = $relationshipOptions->sortBy($options->sort->field);

I tried $relationshipOptions = $model->get()->sortBy($options->sort->field)->take($on_page)->skip($skip);

It's OK for Page 1, the first 50 datas are sorted by name, but next pages are empty.

I think there is some changes to do between line 911 and 947.

Screenshots

No response

Additional context

No response

ben-cheng565 commented 1 year ago

@toto975 Can I try to work this issue out?

toto975 commented 1 year ago

Certainly you can Great if you find the fix.

toto975 commented 1 year ago

It's fixed with https://github.com/the-control-group/voyager/pull/5695

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.