Variable returns NULL - Trying to get Property of non-object Variable returns NULL

Asked

Viewed 55 times

-2

Somebody help me I’m making this mistake Variable returns NULL - Trying to get Property of non-object

inserir a descrição da imagem aqui

  • 1

    Your example could be written to make life easier for those who are visiting the site to consult the question and to answer you as well. Give a read on these rules here: https://pt.meta.stackoverflow.com/questions/1186/como-crea-example-m%C3%adnimo-completo-e-verific%C3%a1vel? cb=1.

1 answer

1


According to Exception, the instant you try to grab the property $user->id, the variable $user is not an object. Actually, this variable is null.

This is because you are assigning to the variable $user the return of function attach, which is of the void type:

$user = (...)->attach($permissao->id);

To fix the problem, just fragment your code into two parts:

$user = User::create([
    'nome' => $request->nome,
    'email' => $request->email,
    'password' => bcrypt($request->password)
]);

$user->roles()->attach($permissao->id);

// Continua o código normalmente...
  • perfect, gave right.... thank you very much

Browser other questions tagged

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