Add field to standard registration form of Laravel 5.3

Asked

Viewed 444 times

0

Good afternoon,

I want to add the type field to the user of a system in Laravel and am doing as follows:

Migration

   public function up()
    {
      Schema::create('users', function (Blueprint $table) {
         $table->increments('id');
         $table->string('name');
         $table->string('email')->unique();
         $table->string('password');
         $table->smallInteger('type');
         $table->rememberToken();
         $table->timestamps();
     });
    }

Register.blade.php (I added a select to type):

<div class="form-group">
  <label for="type" class="col-md-4 control-label">Tipo de usuário: </label>
  <div class="col-md-6">
    <select class="form-control" name="type" style="width:350px">
      <option value="1">Cliente</option>
      <option value="2">Funcionário</option>
      <option value="3">Gerente</option>
    </select>
 </div>

Registercontroller:

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'type' => 'required',
            'password' => 'required|min:6|confirmed',
        ]);
    }
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'type' => $data['type'],
            'password' => bcrypt($data['password']),
        ]);
    }

The user is saved but always with type 0 and I don’t understand why

1 answer

1


I believe the problem is in the fillables of your Model User, I recommend two changes:

1 - Improve Migration to have a default value on type.

   public function up()
    {
      Schema::create('users', function (Blueprint $table) {
         $table->increments('id');
         $table->string('name');
         $table->string('email')->unique();
         $table->string('password');
         $table->smallInteger('type')->default(1);
         $table->rememberToken();
         $table->timestamps();
     });
    }

2 - It is necessary to review your model, which must have fillable attribute with all columns of the database.

 protected $fillable = ['name', 'email', 'password', 'type'];

So create, update and related methods should work perfectly.

Browser other questions tagged

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