插入MySQL时如何加密密码?

时间:2014-06-01 17:41:07

标签: php mysql laravel-4

我正在尝试使用Laravel 4创建用户注册表单,但是在插入数据库之前我无法加密密码。我的示例代码如下。

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);
        }

}

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');
            }
}

routes.php上的代码

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

提前致谢。

1 个答案:

答案 0 :(得分:1)

documentation所述,您必须使用Hash::make功能。

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

那就是说,你的代码摘录表明你并不了解Laravel的工作方式。您是否阅读过文档或任何教程?