Sort order of morphToMany records

Hi! Is it possible to sort records of a morphToMany relationship the way a belongToMany relationship allows? Working example for belongToMany relationship (test plugin):

// october\test\models\Gallery.php

use \October\Rain\Database\Traits\SortableRelation;

public $belongsToMany = [
    'countries' => [
        Country::class,
        'table' => 'october_test_galleries_countries',
        'conditions' => '1 = 1',
        'pivotSortable' => 'sort_order',
    ]
];

I added a sort_order column to the october_test_gallery_entity table, but it didn’t work. Example:

// october\test\models\Post.php

use \October\Rain\Database\Traits\SortableRelation; // added by me

public $morphToMany = [
    'galleries' => [
        Gallery::class,
        'name' => 'entity',
        'table' => 'october_test_gallery_entity',
        'pivotSortable' => 'sort_order', // added by me
    ],
    'galleries_pivot' => [
        Gallery::class,
        'name' => 'entity',
        'table' => 'october_test_gallery_entity',
        'pivot' => ['commission_amount'],
        'pivotSortable' => 'sort_order', // added by me
    ],
];

In config_relation.yaml I added:

structure:
    showReorder: true
    showTree: false

try to use a scope in the each relationships, and then implements the scope function inside the related models. Inside this function, you can actually execute the ordering.

something like (not tested):

public $morphToMany = [
    'galleries' => [
        Gallery::class,
        'name' => 'entity',
        'table' => 'october_test_gallery_entity',
        'scope' => 'MyScopeFunction', 
    ],

and


class Gallery extends model

public function scopeMyScopeFunction($query) {
    $query->order("my_sort_column", "desc");
}

Thank you for your answer.
Your solution works, but it doesn’t allow changing the order of records using drag and drop.

image

I need to be able to reorder the records in the Galleries field individually for each Post.