Laravel Defender Trying to get Property 'name' of non-object

Asked

Viewed 277 times

0

Is returning the following error of my assignment:

inserir a descrição da imagem aqui

$user->attachRole(12);

Observing: if I use this way parameterized the normal functional defender models

$user->roles()->attach(12);

What can it be? If the attachRole does not work, probably the other functions of the Defender won’t work?

1 answer

0

Look at your code, the method attachRole expects an object and not a number.

public function attachRole() {
    if(!$this->hasRole($role->name)) {
        $this->roles()->attach($role);
    }
}

The object must contain a property name, see line 51 of the code (according to the image you entered in the perguta).

Therefore, the code $user->attachRole(12); causes the error below to be triggered:

Trying to get Property 'name' of non-object

Translating, the code is trying to get the property name of an element that is not an object.

The method $user->roles() returns an object that provides the method attach, so it works:

$user->roles()->attach(12);

Surely there is some validation within the method attach which checks if the given value is an object, so no error is triggered, but this only by analyzing the code of the method.

  • I took a look at the Laravel Defender documentation and made some changes $user = $this->create($request->all()); $role = Defender::findRole('Admin'); $user->attachRole($role); now I get the following error "Class 'Illuminate Foundation Auth Defender' not found" I reviewed the entire installation and did according to the Documentation, there are no installation errors!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.