输入数据未发布到数据库

时间:2015-09-18 01:18:37

标签: php laravel

我遇到了一个问题,我有三个字段,三个字段中有两个发布到数据库。我会在下面发布我的代码,看看你是否可以帮助我。

如果您想要完整的代码预览,可以在以下位置找到: http://github.com/mikerodham/chatty

我正在关注CodeCourse / PHPAcademy的社交网络教程,这只是我偶然发现的一个奇怪的错误。

是的,我已经检查过数据库中的用户名字段是否拼写正确!

查看

<form class="form-vertical" role="form" method="post" action="{{ route('auth.signup') }}">

    <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
      <label for="email" class="control-label">Your email address</label>
      <input type="text" name="email" class="form-control" id="email" value="{{ Request::old('email') ?: '' }}">
      @if ($errors->has('email'))
          <span class="help-block">{{ $errors->first('email') }}</span>
      @endif
    </div>

    <div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
      <label for="username" class="control-label">Your desired username</label>
      <input type="text" name="username" class="form-control" id="username" value="{{ Request::old('username') ?: '' }}">
      @if ($errors->has('username'))
          <span class="help-block">{{ $errors->first('username') }}</span>
      @endif
    </div>

    <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
      <label for="password" class="control-label">Your desired password</label>
      <input type="password" name="password" class="form-control" id="password" value="">
      @if ($errors->has('password'))
          <span class="help-block">{{ $errors->first('password') }}</span>
      @endif
    </div>

    <div class="form-group">
      <button type="submit" class="btn btn-default">Sign up</button>
    </div>

    <input type="hidden" name="_token" value="{{ Session::token() }}">

  </form>

CONTROLLER

  public function postSignup(Request $request)
 {
$this->validate($request, [
    'email' => 'required|unique:users|email|max:255',
    'username' => 'required|unique:users|alpha_dash|max:20',
    'password' => 'required|min:6',
  ]);

  User::create([
      'email' => $request->input('email'),
      'username' => $request->input('username'),
      'password' => bcrypt($request->input('password')),
    ]);

    return redirect()->route('home')->with('info', 'Your account has been created and you can now sign in!');

}

1 个答案:

答案 0 :(得分:0)

感谢AndrewMalinnkov的回答!

protected $fillable = [
  'username', // <-- Was not set correctly in my model!
  'email',
  'password',
  'first_name',
  'last_name',
  'location',
];