Answer the question
In order to leave comments, you need to log in
Laravel fetching models with sorting by relation models?
I want to sort current post models by author's name.
$posts= Post::with(['author' => function ($query) {
$query->orderBy('name', 'desc');
}])->get();
0 => array:3 [▼
"query" => "select * from "posts" where "posts"."deleted_at" is null limit 3"
"bindings" => []
"time" => 1.13
]
1 => array:3 [▼
"query" => "select * from "authors" where "authors"."id" in (?, ?, ?) order by "name" desc"
"bindings" => array:3 [▼
0 => 1
1 => 5
2 => 11]
"time" => 3.99
]
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question