User registration and inserting DB with Laravel

时间:2018-11-13 07:52:15

标签: php laravel laravel-5

I setup registration form. connected with request and controller. But I couldn't save the data to DB's users table. it's connected to DB, because there is one user data in the DB and when I use that's email for a new registration, it's not accepting it. But when I try to create new user no data save into DB table.

requestController

public function authorize()
    {
        return true;
    }

public function rules()
    {
        return [
            'name' => 'required|min:3|max:20',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|min:6|max:20|confirmed',
            'password_conformation' => 'required|min:6|max:20'
        ];
    }

Controller

public function register(registerRequest $request)
    {
        $request->flash();

        $request['password'] = bcrypt($request->password);

        $user = new User;
        $user->fill($request->all());
        $user->save();

        return view('auth.register')->withErrors($request);
    }

and my User Model

/**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'users';

    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

if you need to see form as well, i can update that code here too.. Thank you for help!

Update

form code

<form class="form-horizontal" role="form" method="post" action="{{url('/auth/register')}}">
                {{csrf_field()}}
                <div class="row">
                    <div class="col-md-3"></div>
                    <div class="col-md-6">
                        <h2>新ユーザ</h2>
                        <hr>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 field-label-responsive">
                        <label for="name">名前</label>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                                <div class="input-group-addon" style="width: 2.6rem"><i class="fa fa-user"></i></div>
                                <input value="{{old('name')}}" type="text" name="name" class="form-control" id="name"
                                       placeholder="田中 松本" required autofocus>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-control-feedback @if ($errors->first('name')) {{'text-danger'}} @endif">
                        <span class="text-danger align-middle">
                           {{$errors->first('name')}}
                        </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 field-label-responsive">
                        <label for="email">E-Mail Address</label>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                                <div class="input-group-addon" style="width: 2.6rem"><i class="fa fa-at"></i></div>
                                <input value="{{old('email')}}" type="text" name="email" class="form-control" id="email"
                                       placeholder="you@example.com" required autofocus>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-control-feedback @if ($errors->first('name')) {{'text-danger'}} @endif">
                        <span class="text-danger align-middle">
                           {{$errors->first('email')}}
                        </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 field-label-responsive">
                        <label for="password">Password</label>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group has-danger">
                            <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                                <div class="input-group-addon" style="width: 2.6rem"><i class="fa fa-key"></i></div>
                                <input type="password" name="password" class="form-control" id="password"
                                       placeholder="Password" required>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-control-feedback @if ($errors->first('name')) {{'text-danger'}} @endif">
                        <span class="text-danger align-middle">
                           {{$errors->first('password')}}
                        </span>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3 field-label-responsive">
                        <label for="password_confirmation">Confirm Password</label>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <div class="input-group mb-2 mr-sm-2 mb-sm-0">
                                <div class="input-group-addon" style="width: 2.6rem">
                                    <i class="fa fa-repeat"></i>
                                </div>
                                <input type="password" name="password_confirmation" class="form-control"
                                       id="password-confirm" placeholder="Password again" required>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3"></div>
                    <div class="col-md-6">
                        <button type="submit" class="btn btn-success"><i class="fa fa-user-plus"></i> Register</button>
                    </div>
                </div>
            </form>
        </div>

1 个答案:

答案 0 :(得分:0)

您的密码确认字段在rules函数中的拼写错误:  'password_conformation'应该是password_confirmation