Php Laravel 5.7访问器,主要基于一些可能的匹配检查与身份验证用户的相关性

Php Laravel 5.7访问器,主要基于一些可能的匹配检查与身份验证用户的相关性,php,laravel,laravel-5,eloquent,laravel-5.7,Php,Laravel,Laravel 5,Eloquent,Laravel 5.7,我的模型上有一些作用域用于relativeTo或notRelativeTo: public function scopeRelativeTo($query, User $user) { $teamIDs = $user->teams->pluck('id'); $query->where('assigned_user_id', $user->id) ->orWhere('reported_by', $user->id)

我的模型上有一些作用域用于
relativeTo
notRelativeTo

public function scopeRelativeTo($query, User $user)
{
    $teamIDs = $user->teams->pluck('id');

    $query->where('assigned_user_id', $user->id)
        ->orWhere('reported_by', $user->id)
        ->orWhereIn('assigned_team_id', $teamIDs);
}


public function scopeNotRelativeTo($query, User $user)
{
    $teamIDs = $user->teams->pluck('id');

    $query->where('assigned_user_id', '!=', $user->id)
        ->orWhere('reported_by', '!=', $user->id)
        ->orWhereNotIn('assigned_team_id', $teamIDs);
}
这些有助于基于可能的匹配构建查询,这些匹配基本上可以归结为用户是否与对象具有相关性。这些对于查询来说非常好,但是我也尝试在模型上创建一个访问器,它将返回一个布尔值,这样每个对象都将动态地知道它与经过身份验证的用户的相关性。好主意,但不知道从哪里开始

public function getRelativityAttribute($value)
{
    $user = User::find(auth('api')->user()->id);
}
对于经过身份验证的用户,并且知道我也有权访问实际类:
$this
,我如何基于范围中的相同检查检查相关性并返回布尔值?

尝试以下操作:

public function getRelativityAttribute($value)
{
    $user = auth('api')->user();
    if (!$user) {
        return false;
    }

    $userId = $user->getKey();
    return $this->assigned_user_id == $userId || $this->reported_by == $userId
           || $user->teams()->where('id', $this->assigned_team_id)->exists();
}
试试这个:

public function getRelativityAttribute($value)
{
    $user = auth('api')->user();
    if (!$user) {
        return false;
    }

    $userId = $user->getKey();
    return $this->assigned_user_id == $userId || $this->reported_by == $userId
           || $user->teams()->where('id', $this->assigned_team_id)->exists();
}

这似乎对所有情况都是真的,尽管有明显的不应该出现的情况。我已经更新了我的答案,最后一个条件应该没有
似乎对所有情况都返回true,尽管存在明显的不应该返回的实例。我已经更新了我的答案,最后一个条件应该没有