简体   繁体   中英

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. My sample code is as below.

Code at 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

    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

    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.

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

That said, your code extracts show that you don't have understood the way Laravel work. Have you read the documentation or any tutorial ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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