jeudi 18 octobre 2018

Laravel 5.7 accessor that essentially checks the relativity to the auth user based on a few possible matches

I have some scopes on my model for relativeTo or 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);
}

These help build a query based on a possible matches that essentially boil down whether a user has relativity to the object or not. These are great for quering, but I'm also trying to create an accessor on the model that will return a boolean so each object will dynamically know it's relativity to the authenticated user. Fine idea, but not sure where to even start.

public function getRelativityAttribute($value)
{
    $user = User::find(auth('api')->user()->id);
}

With the authenticated user, and knowing that I also have access to the actual class: $this, how might I check the relativity based on those same checks in the scopes and return a boolean value?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire