拉拉维尔用户::创建缺失的数据


Laravel user::create missing data

我已使用迁移来更新我的用户表以包含已提交的用户名,但在注册时,该字段未填充在数据库中:

    $input = Input::only(['name', 'username', 'email', 'password']);
    $input['password'] = Hash::make($input['password']);
    User::create($input);
    return Redirect::to('login');

帖子的转储提供了以下内容:

array (size=4)
  'name' => string 'Joe Bloggs' (length=13)
  'username' => string 'jbloggs' (length=12)
  'email' => string 'jbloggs@gmail.com' (length=24)
  'password' => string '$2y$10$whgTTwa5g.du/WJfJGhGtO******SYTerzL4bhOuedW4C' (length=60)

但是,用户名字段始终为空。没有错误或警告。我已经尝试了回滚和重新迁移,但没有变化。

我错过了什么吗?谢谢!

传递给create的所有属性都需要在 $fillable 数组中定义,以便您可以使用它们进行质量分配

在您的情况下:

protected $fillable = array('name', 'username', 'email', 'password');

或者,您也可以定义相反的一组guarded属性:

protected $guarded = array('foo', 'bar');

当您在eloquent model中添加用户时,您有一个数组$fillable允许您批量分配更多可以在 http://laravel.com/docs/4.2/eloquent#mass-assignment