Open connecteev opened 1 year ago
Maybe not what you expect to happen, but not really a bug. This package uses the https://github.com/spatie/eloquent-sortable package and when Move To Start is clicked this method is called: https://github.com/spatie/eloquent-sortable/blob/main/src/SortableTrait.php#L152
On line 167 all other order columns are incremented after updating the order column of the clicked model. The sort order still is correct, but the sort order values may not be what you expect them to be.
If you want to set a new order for all the records after moving one to the top, use this: https://github.com/spatie/eloquent-sortable/blob/main/src/SortableTrait.php#L44
Example code:
use Spatie\EloquentSortable\SortableTrait;
class Example extends Model implements Sortable
{
use SortableTrait {
moveToStart as traitMoveToStart;
}
public function moveToStart(): static
{
$this->traitMoveToStart();
$this->setNewOrder($this->buildSortQuery()->ordered()->pluck($this->primaryKey)->toArray());
return $this;
}
}
Problem:
Using the "Move to Top" or "Move to End" buttons does not work as expected and creates incorrect sort order values after the sorting is done. Note: Using Drag-and-Drop to sort / reorder rows in a Nova resource does not cause this problem (though auto-refresh is broken and the page has to be manually refreshed).
Bug Description:
Model relationships:
Goal:
To allow CourseSections to be sortable WITHIN a Course, using drag-and-drop on the Course resource Detail page in Nova. All the CourseSections under /admin/resources/courses/1 should be sortable. In the example below, there are 4 sections in course 1, and 4 sections in course 2. The sort values for the sections should be between 1-4 at all times.
Steps:
Database screenshot - BEFORE (course_sections table):
Video Demo of the problem:
https://github.com/outl1ne/nova-sortable/assets/64816/5389ad4d-d93b-4ec4-819a-8ea107e49b9a
Database screenshot - AFTER (course_sections table): Note that we only updated /admin/resources/courses/1 . Although we did not go to /admin/resources/courses/2 and update the sort for course # 2, the order values are now messed up:
The sort values for the sections should be between 1-4 at all times. Instead, we see values like 5, 6, and multiple CourseSection (rows in course_sections table) have the same value of 4.
Here is my code for the models and Nova resources: