0

I have been searching for this and I cannot find an answer. The Laravel docs are a little vague.

I am trying to register a user and for the most part it is working using Laravel's built in authentication tools. All I am trying to do is, instead of just have a name field when a user registers, I want to have a first_name and last_name field.

In order to do this I have created a migration that extends the users table that the defualt laravel migration creates. Here is the code for that:


<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddFirstNameLastNameToUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { // Schema::table('users', function($table) { $table->dropColumn('name'); $table->string('first_name')->after('id'); $table->string('last_name')->after('first_name'); }); } /** * Reverse the migrations. * * @return void */ public function down() { // Schema::table('users', function($table) { $table->dropColumn('last_name'); $table->dropColumn('first_name'); $table->string('name'); }); } } 

So after that, I created the view for the registration form. Here is the code for that:


@extends('../master') @section('content') <div class="container"> <div class="row"> {!! Form::open(['url' => url('auth/register'), 'method' => 'post']) !!} <div class="form-group"> {!! Form::label('email', 'Email') !!} {!! Form::email('email', null, ['class'=>'form-control', 'id'=>'username']) !!} </div> <div class="form-group"> {!! Form::label('first_name', 'First Name') !!} {!! Form::text('first_name', null, ['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('last_name', 'Last Name') !!} {!! Form::text('last_name', null, ['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('password', 'Password') !!} {!! Form::password('password', ['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::label('password_confirmation', 'Password Confirmation') !!} {!! Form::password('password_confirmation', ['class'=>'form-control']) !!} </div> <div class="form-group"> {!! Form::submit("Register") !!} </div> {!! Form::close() !!} </div> </div> @stop 

It the says the validate function in the auth controller will validate the input, so here is the code for that:


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

Now all this seems to work fine the problem is in the create function in the auth controller. The auth method takes in an array of data, The laravel 5.1 docs dont say what is going to be in this array. I assumed it would be the form fields that were in the registration form but I am not getting the first_name and last_name field that are in registaction form. It must be validating correctly cause entries are getting saved in the DB but the first_name and last_name columns are not getting saved. I did a dd on the data array but its not dying so I don't know what is in the array and how to get information in that array.

Side note, the laravel docs also dont say where errors will be saved when a registration validation fails so I dont know how to display registration errors.

edit:

Here is the model, the first_name and last_name fields weren't fillable. I then made them fillable at the suggestion of @Andrew, it is still not filling the fields.


<?php namespace SellIt; use Illuminate\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract { use Authenticatable, Authorizable, CanResetPassword; /** * The database table used by the model. * * @var string */ protected $table = 'users'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = ['first_name', 'last_name', 'email', 'password']; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = ['password', 'remember_token']; } 
6
  • What does your model look like? I'm guessing that's where the problem is. Make sure the new fields you create are fillable. Also you could simply modify the create_users_table migration, no need to create another one. Commented Dec 22, 2015 at 10:01
  • I thought the fillable thing would do it but it didn't :( . I was going to edit the initial migration but once you start editing migrations instead of creating new ones you can start running into problems. Commented Dec 22, 2015 at 10:09
  • I don't see why you would run into problems. If you simply modify the migration there's nothing wrong with that, in fact I see it as a better idea than creating another migration for a table that already exists. Commented Dec 22, 2015 at 10:10
  • Do you get any data in the Input::all()? Also remove your validator for now, it's only getting in the way. Just comment it out or something. Commented Dec 22, 2015 at 10:11
  • The problem with editing existing migrations comes when you are working in a team and have multiple people trying to use the same set of migrations, you could end up with different versions of migrations and different versions of database, its best to just create a new migration. But thank you, I think the only thing I needed to do was the fillable thing. Commented Dec 22, 2015 at 10:16

1 Answer 1

0

So... funny story. It was showing a row in MySQL workbench so I would right click on the row, hit delete and it would show the table as empty. Turns out MySQL Workbench was not actually deleting the record. I manually deleted the record with a query and registered again and it worked. Thanks for your help guys.

Sign up to request clarification or add additional context in comments.

1 Comment

In workbench you have to click apply after making changes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.