Undefined index Laravel

Asked

Viewed 1,863 times

0

When performing a request I’m getting undefined index.

The code in question:

protected function create(array $data)
    {

        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'telefone' => $data['telefone'],
            'usuario_anjo' => $data['usuario_anjo'],
            'password' => bcrypt($data['password']),
        ]);
    }

My class:

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'password_confirmation', 'telefone', 'usuario_anjo'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token', 'password_confirmation'
    ];
}

My request:

email: "[email protected]"
​
name: "teste"
​
password: "teste"
​
telefone: "(16)98182-4833"
​
usuario_anjo: 0

the error in question:

message: "Undefined index: usuario_anjo"

My column in the bd is defined as int. Could someone please tell me why this mistake?

  • Before calling the create function uses this here: echo '<pre>'; print_r( $data ); die; E put here the return.

1 answer

0


Ps: in this answer I assume you are overwriting the RegisterController.

Your code is correct only that Laravel is not passing the usuario_anjo because it is not in the validation, and the information passed to the method create within the $data are only validated information.

So add this method in the same class where your method is create:

protected function validator(array $data)
{

    return Validator::make($data, [
        'name' => 'required|string',
        'email' => 'required|email|unique:users',
        'password' => 'required|min:6|confirmed',
        'telefone' => 'required|string',
        'usuario_anjo' => 'required|integer'
    ]);
}

This will overwrite the function validator original call when validating the request and add validation to the usuario_anjo, that way, when Laravel calls out $request->validated(), the usuario_anjo will be included.

Browser other questions tagged

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