How to sort the dropdown in a list filter

I have 2 models: Property and Country. Each Property belongs to a country.

public $belongsTo = [
        'country' => [
            \Jlh\Property\Models\Country::class,
            'order' => 'name asc'
        ],
];

On the Property backend list, I have a filter for Country but I can’t get the countries to list in alpha order:

scopes:
  country:
      label: Country
      modelClass: Jlh\Property\Models\Country
      conditions: country_id in (:value)
      nameFrom: name

Any help is appreciated.

Hello @jumpshotink

i think you can add the key orderBy in your relationship, something like
'orderBy' => 'name'

Hi Chris,

Thanks for the reply. I have the ‘order’ => ‘name asc’ in the model. The issue is when you use a scope (in this case for countries), the list of countries are not ordered by name. Here is an attachment.

So I am trying to figure out how to order the countries in the pull down of the country scope.

Hi Chris! Use the optionsMethod or modelScope to return a custom sorted array or collection. Take a look at: Dropdown Scope - October CMS - 3.x

Found a solution.

The scope.yaml file:

scopes:
  country:
      label: Country
      modelClass: Jlh\Property\Models\Country
      conditions: country_id in (:value)
      nameFrom: name
      options: getSortedCountryOptions

The Country.php model:

public function getSortedCountryOptions()
    {
        return Country::orderBy('name')->pluck('name', 'id')->toArray();
    }

Hope this is helpful.