Wednesday, May 16, 2018

Empty model in Eloquent Global scope? - development

Empty model in Eloquent Global scope?

I am trying to determine if a user has the ability to update a model in a global scope, but I am using permission prefixes that I normally get through a relation.

The apply code is as follows:

public function apply(Builder $builder, Model $model)
{
    $user = getUser();

    if ($user === null || $user->cannot('update', $model)) {
        $builder->where('active', '=', 1);
    }
}

When I dd($model) the model is not actually instantiated so when I do my update permission check in my policy:

public function update(User $user, Item $item)
{
    return $user->hasAnyPermission(['edit-things', $this->prefix($item) . "-edit-item"]);
}

Where the prefix function looks like:

private function prefix(Item $item = null)
{
    if ($item !== null) {
        return $item->parentRelation->roles_permission_prefix;
    }
    $parentRelation= ParentRelation::findOrFail(request('parent_relation_id'));
    return $parentRelation->roles_permission_prefix;
}

It all fails due to their actaully not being a relationship. Any ideas?

Quick Edit: I am using the Spatie Permissions library if that is pertinent.



from Laravel Questions and Answers https://laravelquestions.com/laravel/empty-model-in-eloquent-global-scope/
via Lzo Media

No comments:

Post a Comment