E
E
Evgeny Perin2016-08-01 16:28:47
Laravel
Evgeny Perin, 2016-08-01 16:28:47

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');
}

But the seasons are sorted somehow randomly, but the episodes in the seasons are normal.
Help with the request, it can sort not a collection, but something else when requested, but nothing sensible has come out so far
UPD:
I forgot to say, it’s not just necessary to have such an order, you also need grouping by seasons. That is, in order to display all the seasons in a cycle, inside each season there is another cycle with series

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Pochepko, 2016-08-01
@seoperin

Try

return $this->episodes()
->active()
->groupBy('season')
->orderBy('season', 'DESK')
->orderBy('number', 'DESK')
->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question