简体   繁体   English

插入MySQL时如何解密密码?

[英]How do I bcrypt password when inserting into MySQL?

I am trying to create a user registration form using Laravel 4 but I am having trouble to bcrypt the password before inserting into the database. 我正在尝试使用Laravel 4创建用户注册表单,但是在插入数据库之前无法对密码进行加密。 My sample code is as below. 我的示例代码如下。

Code at app/model/Register.php app / model / Register.php中的代码

    class Register extends Eloquent {


        protected $guarded = array();
        protected $table = 'login'; // table name
        public $timestamps = 'true' ; // to disable default timestamp fields

        // model function to store form data to database
        public static function saveFormData($data)
        {
            DB::table('login')->insert($data);
        }

}

Code at RegisterController.php RegisterController.php上的代码

    public function store()
{
            $data =  Input::except(array('_token')) ;
            $rule  =  array(
                    'firstName'=>'required|alpha|min:2',
                    'lastName'=>'required|alpha|min:2',
                    'email'=>'required|email|unique:intern_login',
                    'password'=>'required|alpha_num|min:8'
                ) ;

            $validator = Validator::make($data,$rule);

            if ($validator->fails())
            {
                    return Redirect::to('register')
                            ->withErrors($validator->messages());
            }
            else
            {

                    Register::saveFormData(Input::except(array('_token')));

                    return Redirect::to('register')
                            ->withMessage('Data inserted');
            }
}

Code at routes.php 在routes.php中的代码

    Route::post('register_action', function()
{
        $obj = new RegisterController() ;
        return $obj->store();
});

Thanks in advance. 提前致谢。

As said in the documentation , you have to use the Hash::make function. 文档中所述,您必须使用Hash::make函数。

$password = Hash::make('secret');

That said, your code extracts show that you don't have understood the way Laravel work. 也就是说,您的代码摘录表明您不了解Laravel的工作方式。 Have you read the documentation or any tutorial ? 您阅读过文档或任何教程吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM