Answer the question
In order to leave comments, you need to log in
Sorting a collection by two parameters?
There is a Serial model and there is an Episode model. Episode in the table has number (series number) and season (season number). I need to get a collection (array?) to iterate over these two fields, so that the latest seasons and latest episodes come first and end with the first episode of the first episode:
6 season 24 episode
6 season 23 episode
....
1 season 2 episode
1 season Episode 1
The series made a method:
public function list()
{
return $this->episodes()->active()->get()->sortByDesc('number')->groupBy('season')->sortByDesc('season');
}
Answer the question
In order to leave comments, you need to log in
Try
return $this->episodes()
->active()
->groupBy('season')
->orderBy('season', 'DESK')
->orderBy('number', 'DESK')
->get();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question