Here is a good article for that
https://reinink.ca/articles/ordering-database-queries-by-relationship-columns-in-laravel
By the way I always use joins If I need that.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I have this code and I wonder if it is possible to do something to order my documents according to the ordre field of the join table without using join.
$documents = Document::
with('doctype')
->where('formation_id', $this->formation_id)
->get();
And I would like to orderBy('doctypes.ordre') or orderByRaw('doctypes.ordre').
$documents = Document::
with('doctype')
->where('formation_id', $this->formation_id)
->orderBy('doctypes.ordre')
->get();
But now the query tries to find a doctypes.ordre field in the documents table.
I know how to do with join, but do you know another way to do without using join ?
Thank you for your help.
Vincent
Here is a good article for that
https://reinink.ca/articles/ordering-database-queries-by-relationship-columns-in-laravel
By the way I always use joins If I need that.
Please or to participate in this conversation.