0
votes

I'm trying to learn how to use Laravel and until now I haven't had any problem. Then I tried to implements an example of Authentication.

1) I created a migration file where i set the User table (password is set to 60, the token is set to 100 following the instructions on laravel site)

2) I used a seeder to generate fake users:

$faker = Faker::create();

        foreach(range(1, 10) as $index)
        {
            User::create([
                'firstname' => $faker->firstName(),
                'lastname'  => $faker->lastName(),
                'email'     => $faker->email(),
                'password'  => Hash::make('password'),
                'telephone' => $faker->phoneNumber(),
                'admin'     => rand(0,1)
            ]);
        }

The table in the database is perfectly seeded without problem.

3) I created a controller UserController with the following function

public function postCreate() { $validator = Validator::make($data = Input::all(), User::$rules);

if ($validator->fails()) {
    return Redirect::to('users/newaccount')->with('message','error')->withInput()->withErrors($validator);
}

unset($data['password_confirmation']);
$data['password'] = Hash::make(Input::get('password'));
User::create($data);

return Redirect::to('users/signin')->with('message','thanks');

}

4) I created a form for register the user

{{ Form::open(array('route'=>array('users.createaccount'), 'method' => 'post')) }}
        .....
            {{ Form::label('password') }}
            {{ Form::password('password') }}
        ....
        {{ Form::submit('Create new account') }}
        {{ Form::close() }}

Problem is when i check on the DB table the user is register and saved, and all the fields are filled except for the password (and the token that remains null)..

But if i do a dd($data) before calling User::create($data) this is what i can see:

array(6) { ["_token"]=> string(40) "5bSkWYHLfeBh8E2agiB15J2cQSzs6orI7m5ruFhx" ["firstname"]=> string(4) "mark" ["lastname"]=> string(4) "mark" ["email"]=> string(13) "[email protected]" ["password"]=> string(60) "$2y$10$J3eM3nBZ0l8eNa1BxpoDc.BzeQUKzTc7dwwbu7g7GdQLj4gJgjWxe" ["telephone"]=> string(8) "12345678" }

Anyone can tell me why only the password and the token fields are not saved with the Eloquent method in the database, while they are saved using the seeder?

thk

1
Is password present in the $fillable array?lukasgeiter
uuuuuu..... yep, that's right... it was that,... do i also have to set the token inside the fillable array?... (how do i vote the answer?)Ivan_nn2

1 Answers

1
votes

All fields you want to pass to create need to be defined inside the $fillable array. Also password

protected $fillable = array('firstname', 'lastname', 'email', 'password', 'telephone', 'admin');
                                                                  ^^

_token definitely doesn't need to be in there. It's just a token Laravel adds for CSRF protection