Skip to content

Empty model in attach{Model} and detach{Model} given #5

Description

@davidraijmakers

I'm currently facing the issue where I want to fix the following:

A User has multiple roles in the Resource as following:
AttachMany::make('Roles', 'roles', Role::class)->required()->rules('required'),

These Roles are Spatie/laravel-permissions roles.

On a User Edit form in Nova I want to check which roles a User can add with the following code:

`public function attachRole(User $user, User $model, Role $role): bool
{
if ($model->hasRole('admin')) {
return false;
}

    if (!$user->hasRole('admin') && $role->name === 'admin') {
        return false;
    }

    if (!$user->hasRole('admin') && $user->id === $model->id) {
        return false;
    }

    if ($user->hasRole('content manager') || $user->hasRole('event manager')) {
        return true;
    }

    return false;
}`

But $model is always an empty User model. i.e.
$user->id === $model->id

Equals 6 === null.

Do you use a new model, or an existing model in a edit form of a resource?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions