dongshi2458 2015-01-02 01:36
浏览 72
已采纳

Laravel用户::创建缺失数据

I've used a migration to update my users table to include a username filed, but on signup the field isn't populated in the database:

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

A dump of the post provides this:

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)

However the username field is always blank. No errors or warnings. I've tried a rollback and re-migrating, but there is no change.

Am I missing something? Thanks!

  • 写回答

2条回答 默认 最新

  • dongwalun2507 2015-01-02 01:42
    关注

    All attributes you pass to create need to be defined in the $fillable array so you can use them for mass assignment

    In your case:

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

    Alternatively you could also define the opposite, a set of guarded attributes:

    protected $guarded = array('foo', 'bar');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部