I'm trying to create a scope on my model that queries for results that are not related to user based on the following possibilities: 1. The assigned_user_id
is not null and reported_by
doesn't match and any of the user's team_ids (one-to-many user to teams) match the assigned_team_id
AND 2. The assigned_user_id
does not match and reported_by
doesn't match and any of the user's team_ids (one-to-many user to teams) match the assigned_team_id
The only tricky part for me really is that the assigned_user_id is nullable. assigned_team_id is required and reported_by is required. This is my current attempt:
public function scopeNotRelativeTo($query, User $user)
{
$teamIDs = $user->teams->pluck('id');
$query->whereNull('assigned_user_id')
->orWhere('assigned_user_id', '!=', $user->id)
->where('reported_by', '!=', $user->id)
->whereNotIn('assigned_team_id', $teamIDs);
}
This is returning 46 results and I am expecting 41, so it's not quite right. The toSql()
is
"select * from \"bugs\" where (\"assigned_user_id\" is null or \"assigned_user_id\" != ? and \"reported_by\" != ? and \"assigned_team_id\" not in (?, ?)) and \"bugs\".\"deleted_at\" is null order by \"created_at\" desc"
which looks close, but it is still clearly imperfect. I can definitely see that there are results slipping through the cracks with matching team_ids mostly. Pseudo code version would be Give me results where the assigned_user_id is not null or matching ? AND reported_by doesn't match ? AND assigned_team_id not in (?, ?)
.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire