我创建了一个简单的用户注册应用程序:
$confirmation_key = str_random(100);
$data = [
'email' => $post['email'],
'password' => Crypt::encrypt($post['password']),
'confirmation_key' => $confirmation_key
];
User::create($data);
当然,我为它添加了一个迁移:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('confirmed')->default(0);
$table->string('confirmation_key')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
用户已成功添加到数据库中,但确认密钥仍然为空,没有更改。其他问题:如果我想向用户表中添加另一列,该怎么办?我需要做什么?
您需要确保已将confirmation_key
添加到User
模型的$fillable
属性中,如下所示:
protected $fillable = ['email','password','confirmation_key'];
如果您想添加另一列,则需要创建新的迁移并运行它;如果您想使此列可填充,则需要将其添加到User
模型的$fillable
属性中。
编辑
如果您想使用fill
方法的create
使任何列不可填充,您可以将其添加到表中,而不可能简单地执行以下操作:
User::create($data);
在这种情况下,你需要这样的东西:
// here you fill fillable data
$user = new User($data);
// this way you can fill properties that are not fillable
$user->some_not_fillable_property = 'some value';
// now you save it to database
$user->save();