Undefined index Laravel

0

When I make 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: user_year"

My column in bd is set to int . Could someone please tell me why this error occurred?

    
asked by anonymous 15.09.2018 / 17:22

1 answer

0

Ps: In this answer I assume you are overwriting RegisterController .

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

In this way, add this method to the same class where your create method is:

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 will add validation to usuario_anjo , so when Laravel calls $request->validated() , usuario_anjo is included.

    
15.09.2018 / 19:55