用户数据检查是否已存在

时间:2015-05-25 11:44:29

标签: laravel-4

我在这里有点新鲜,我刚刚开始使用Laravel。

我有一个注册表,但现在我可以使用已存在的相同用户名注册。

我不想让用户使用相同的用户名注册。我该如何解决这个问题?

  

这是我的注册管理员:

    <?php

class RegisterController extends \BaseController {


    protected $layout = 'register';


    /* === VIEW === */
    public function index()
    {

    }

    public function register()
    {

        // Make's a message if something is required
        $messages = array(
            'required' => 'The :attribute field is required!',
        );

        // Validate the info, create rule for the inputs
        $rules = array(
            'username' => 'required', // Make sure the username field is not empty
            'password' => 'required|min:3', // password has to be greater than 3 characters
            'email'    => 'required' // Make sure the email field is not empty
        );

        // Run the validation rules on the inputs from the form
        $validator = Validator::make(Input::all(), $rules, $messages);

        // If the validator fails, redirect back to the form
        if ($validator->fails()) {
            return Redirect::to('register')
                ->withErrors($validator) // Send all the errors back to the form
                ->withInput(Input::except('password')); // Send back the input (not the password) so that we can repopulate the form
        } else {

            // store
            $user = new User;
            $user->username     = Input::get('username');
            $user->password     = Hash::make(Input::get('password'));
            $user->email        = Input::get('email');
            $user->ip           = Request::ip(); 
            $user->created_at   = Carbon::now();
            $user->save();

            // redirect
            Session::flash('message', 'Account has been created!');
            return Redirect::to('/');
        }
    }
}

实际上我想要的是,想要注册的人不能注册已经存在的用户名和电子邮件。

抱歉我的英语不好:$

希望你能帮助我。

0 个答案:

没有答案