如何根据关系对 Lucid Models 结果进行排序?

How to order Lucid Models results based on relation?

我想要的是根据关系(即 belongsTo 相同 ServiceCategory)对所有 Service 模型结果进行排序,例如 Laravel确实如此:

const services = await Service
  .query()
  .orderBy('categories.id', 'desc')
  .fetch()

问题是 Lucid 不理解它并给出以下错误:

select * from services order by categories.id asc - ER_BAD_FIELD_ERROR: Unknown column 'categories.id' in 'order clause'

这是服务模型:

const Model = use('Model')

class Service extends Model {
  categories() {
    return this
      .belongsToMany('App/Models/ServiceCategory')
      .withTimestamps()
  }
}

module.exports = Service

这是 ServiceCategory 模型:

const Model = use('Model')

class ServiceCategory extends Model {
  services() {
    return this
      .belongsToMany('App/Models/Service')
      .withTimestamps()
  }
}

module.exports = ServiceCategory

我怎样才能让它发挥作用?

尝试使用with()

const services = await Service
  .query()
  .with('categories')
  .orderBy('categories.id', 'desc')
  .fetch()

如果不起作用,请使用 innerJoin