Reputation: 12857
I have a football events table that shows events based on minute
and extra_minute
. For example:
id | type | minute | extra_minute | reason
1 | yellowcard | 45 | null | null
2 | yellowcard | 45 | 1 | null
3 | period | 45 | null | HT
4 | goal | 45 | 2 | null
5 | goal | 46 | 2 | null // this is second half
6 | period | 90 | null | FT
7 | goal | 90 | 2 | null
The order should be
yellowcard (45)
yellowcard (45+1)
goal (45+2)
HT
goal (46)
goal (90+2)
FT
I tried a relationship query in Match like
public function events() {
return $this->hasMany(Event::class)->orderBy('minute', 'asc')
->orderBy('extra_minute', 'asc');
}
But this results in
HT
yellowcard (45)
yellowcard (45+1)
goal (45+2)
...
What is the way of sorting the desired style with Laravel Query Builder? I know why my query builder is not working but I couldn't come up with something to achieve what I want
Upvotes: 0
Views: 52
Reputation: 12857
->orderByRaw('type = ? asc', 'period')
is the way to do it.
return $this->hasMany(Event::class)
->orderBy('minute', 'asc')
->orderByRaw('type = ? asc', 'period')
->orderBy('et_minute', 'asc');
Upvotes: 1