Search code examples
phpeloquentslimslim-2

How to hash password on saving using slim2 and eloquent


I am doing registration for user in that i want password to be stored in hash format using eloquent and slim but i am getting hash not found error This is the error image

In config.php

$database = [
'driver'    => 'mysql',
'host'      => '127.0.0.1',
'database'  => 'onboard',
'username'  => 'root',
'password'  => 'rootpwd',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
];
use Illuminate\Support\Facades\Facade;
use Illuminate\Database\Capsule\Manager as Capsule;
$Capsule = new Capsule;

$Capsule->addConnection($database);

// // Set the event dispatcher used by Eloquent models... (optional)
// use IlluminateEventsDispatcher;
// use IlluminateContainerContainer;
// $capsule->setEventDispatcher(new Dispatcher(new Container));

// Make this Capsule instance available globally via static methods... (optional)
$Capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$Capsule->bootEloquent();

In User.php class

function register_user($first_name,$last_name,$email,$password,$email_verification_sendtime,$email_verification_expiredtime,$created_date) {
    //echo 'hi';
    $result = array();
    $result['status'] = FAILED;
    $user = new User();
    $user->first_name = $first_name;
    $user->last_name = $last_name;
    $user->email = $email;
    $user->password = Hash::make($password);
    $user->email_verification_code = $email_verification_sendtime;
    $user->email_verification_send_datetime = $email_verification_expiredtime;
    $user->created_date = $created_date;
    if($user->save()) {
        //echo 'hi';exit;
        $result['status'] = SUCCESSFULL;
    } 
    return $result;
}

In the above code i am not able to hash the password to store.Can anyone say how to store password as hash please..


Solution

  • Try to replace this line:

    $user->password = Hash::make($password);
    

    with

    $user->password = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);