Native php call function in Laravel Framework

- 1 answer


I use Native PHP or regular PHP. I want to call function bcrypt in Laravel

My code is like this :

    $password = '12345678';
    echo bcrypt($password);

It's not working

Its error is Fatal error: Call to undefined function bcrypt() in...

How to call function bcrypt in Laravel?

Does it can be done?

Thank you




To start off with this isn't a very good idea. If you wanting to use Laravel use Laravel if not just make use of the appropriate libraries to do the job your wanting.

However this is one way to use the BcryptHasher from Laravel ( Not the best way just a way ).

require __DIR__ . '/vendor/autoload.php';

use Illuminate\Hashing\BcryptHasher;

$hasher = new BcryptHasher();


You can't just use the bcrypt method as stuff needs initializing which will take more code than the above.

Also its worth noting that at the end of the day the bcrypt method just does

password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);

so if your not making use of other Laravel stuff just use password_hash the $cost by default in Laravel is 10.