Sorting on related fields?


#1

How do I sort queries by related model field in Lucid? Like select from person p join company c on c.id=p.company_id order by c.name;?
Person.query().with('company').orderBy('company.name') results in ER_BAD_FIELD_ERROR: Unknown column 'company.name' in 'order clause'
And so does with('company', q=>q.select('name')) .


#2

Hey @rag! :wave:

If you need to make complicated queries you better want to use a custom join instead of using with.

Also, note that you are using company.name but you should use companies.name since the name of the table is companies.