Add field to standard laravel 5.3 log form

0

Good afternoon,

I want to add the type field to the user of a laravel system and I'm doing it 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've 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 do not understand why

    
asked by anonymous 12.03.2017 / 16:51

1 answer

1

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

1 - Improve the migration to have a default value in the 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 - You need to review your model, which should have the fillable attribute with all the columns in the database.

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

So create, update and related methods should work perfectly.

    
18.04.2017 / 01:34