Mister Verleg
Mister Verleg

Reputation: 4293

Laravel nova restrict viewAny()

When I want users not to be able to enter an individual resource I can use policies to do the following:

public function view(User $user, Model $object)
{
    if($user->groupName != $object->groupName) {
       return false;
    } else {
       return true;
    }
}

This has as a result that the Components of your group have the eye icon (see red cirle). Components I do not want the user to see dont have the eye icon.

Result

My desired result is that the should not be seen component is not shown at all. How can I achieve this?

I tried:

public function viewAny(User $user)
{
     // $object does not exist here so I cannot use it to filter
     if($user->groupName == $object->groupName) {
       return true;
    } else {
       return false;
    } 
}

Upvotes: 1

Views: 1248

Answers (1)

Patrick Schocke
Patrick Schocke

Reputation: 1491

You need to update the index query of your resource. see more

public static function indexQuery(NovaRequest $request, $query)
{
    return $query->where('groupName', $request->user()->group_name);
}

You should consider updating the relateble query too.

Upvotes: 1

Related Questions