laravel eloquent 保存密码时出错

laravel eloquent error in saving password

我正在努力学习如何使用 Laravel,直到现在我还没有遇到任何问题。 然后我尝试实现一个身份验证的例子。

1) 我创建了一个迁移文件,其中设置了用户 table(密码设置为 60,令牌设置为 100,按照 laravel 网站上的说明)

2) 我用了一个seeder来生成虚假用户:

$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)
            ]);
        }

数据库中的table完美播种没有问题

3) 我创建了一个具有以下功能的控制器 UserController

public 函数 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) 我创建了一个用于注册用户的表单

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

问题是当我检查数据库时 table 用户已注册并保存,并且除了密码(和保持为空的令牌)之外的所有字段都已填写。

但是如果我在调用 User::create($data) 之前执行 dd($data) 这就是我可以看到的:

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

谁能告诉我为什么只有密码和令牌字段没有用 Eloquent 方法保存在数据库中,而使用播种器保存它们?

thk

您要传递给 create 的所有字段都需要在 $fillable 数组中定义。还有password

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

_token 绝对不需要在那里。这只是 Laravel 为 CSRF protection

添加的一个标记