'Ordering Relationship field in CreateOperation - Backpack for Laravel

I wanted to order by name the Categories (relationship field) of my CreateOperation.

It is ordered by default by id.

protected function setupCreateOperation()
    {
        CRUD::setValidation(InscriptionRequest::class);
        CRUD::field('category')->type('relationship')->label('Categorías'); // <--- this field


Solution 1:[1]

try this

...->options((function ($query) {return $query->orderBy('name')->get();}))

or

        CRUD::addField([
        'name'  => 'category',
        'type'  => 'relationship',
        'label' => 'Categorías',
        'options'   => (function ($query) {
            return $query->orderBy('name')->get();
        })


            

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gamer97