php - How to use "where not" in a Laravel Model Relationship? -
i have user table similar to:
=== users === id: int (pk) name: string is_coach: bool ...
and coach request table similar to:
=== coach_requests === id: int (pk) student_id: int(fk => users.id) coach_id: int(fk => users.id) ...
i have corresponding laravel models (i.e. user
, coachrequest
).
in user
model, wish make method such given specified user, return users is_coach = true
, except:
- him/herself ,
- users have been matched person coach in
coach_requests
table.
for example consider following sample data:
users
(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)
now if user with:
- id 1 (i.e. user "a"), return user ids: 2, 3, 5 , 6
- id 2, return user ids: 5, 6
- id 3, return user ids: 4, 5
- id 4, return user ids: 2, 3
- id 5, return user ids: 2, 3, 4
- id 6, return user ids: 2, 3, 4
how can using laravel???
so far have this:
public function scopeoffreecoaches($query) { return $query->where([ 'is_coach' => true, ]); }
so not much!
many thanks!
this raw query on top of head:
select u.id users u u.is_coach = true , u.id <> ’$userid’ , u.id not in( select student_id coach_requests coach_id = ’$userid’ )
quickly done , not tested may have change little.
Comments
Post a Comment