我具有類似於一個用戶表:如何在Laravel模型關係中使用「哪裏不是」?
=== users ===
id: int (PK)
name: string
is_coach: bool
...
和類似於教練請求表:
=== coach_requests ===
id: int (PK)
student_id: int(FK => users.id)
coach_id: int(FK => users.id)
...
我也有相應的Laravel模型(即User
和CoachRequest
)。
在User
模式,我希望做的方法,從而給指定的用戶,回報所有用戶is_coach = true
,除了:
- 他/她和誰已經再配上
- 用戶那人作爲
coach_requests
表中的教練。
例如,考慮下面的示例數據:
用戶
(1, "A", false)
(2, "B", true)
(3, "C", true)
(4, "D", true)
(5, "E", true)
(6, "F", true)
coach_requests
(1, 2, 3)
(2, 2, 4)
(3, 3, 2)
(4, 3, 6)
(5, 4, 5)
(6, 4, 6)
(7, 5, 6)
(8, 6, 5)
(9, 1, 4)
現在,如果我是用戶用:
- id 1(即,用戶 「A」),返回用戶ID:2,3,5和6
- ID 2,返回用戶ID:5,6
- ID 3,返回用戶ID:4,5
- ID 4,返回用戶ID:2,3
- ID 5,回報用戶ID:2,3,4
- ID 6,回報用戶ID:2,3,4
我怎樣才能做到這一點使用Laravel? ??
到目前爲止,所有我的是這樣的:
public function scopeOfFreeCoaches($query) {
return $query->where([
'is_coach' => true,
]);
}
所以沒有太多!
非常感謝!
這是一個原始查詢可能會去,因爲它是一種自定義的最簡單的方法的情況下: https://laravel.com/docs/5.1/database – purpleninja
我什至不知道這是什麼原始的將是 –