我有一个足球比赛表,显示基于minute
和extra_minute
的事件。例如:
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
订单应该是
yellowcard (45)
yellowcard (45+1)
goal (45+2)
HT
goal (46)
goal (90+2)
FT
我在Match中试过了一个关系查询
public function events() {
return $this->hasMany(Event::class)->orderBy('minute', 'asc')
->orderBy('extra_minute', 'asc');
}
但这导致了
HT
yellowcard (45)
yellowcard (45+1)
goal (45+2)
...
使用Laravel Query Builder对所需样式进行排序的方法是什么?我知道为什么我的查询构建器不起作用但我无法想出一些东西来实现我想要的东西
->orderByRaw('type = ? asc', 'period')
是这样做的方式。
return $this->hasMany(Event::class)
->orderBy('minute', 'asc')
->orderByRaw('type = ? asc', 'period')
->orderBy('et_minute', 'asc');