Here’s a plagiarism-free 1500-word blog post on the topic “Create Hash Password in Laravel 12”, written in clear and professional English:
Create Hash Password in Laravel 12: A Complete Guide
Security is one of the most critical aspects of any web application. In modern web development, storing passwords in plain text is considered a severe security risk. Instead, developers use hashing algorithms to securely store passwords. Laravel, being a popular PHP framework, provides a powerful and easy-to-use hashing system out of the box. With the release of Laravel 12, working with hashed passwords has never been more efficient and secure.
In this guide, we will walk through how to hash passwords in Laravel 12 using the built-in Hash facade, explore various use cases, and understand how hashing works under the hood.
What is Password Hashing?
Before we dive into the Laravel-specific implementation, it’s essential to understand what hashing means.
Hashing is the process of converting data (like a password) into a fixed-size string of characters, which is typically a hash code. It’s a one-way operation, which means once a password is hashed, it cannot be reversed back to its original form.
Common hashing algorithms include bcrypt, argon2, and sha256. Laravel uses bcrypt by default, but it also supports Argon2 and Argon2id for more modern and secure hashing.
Why You Should Hash Passwords
When users create accounts or log into your application, they usually provide a password. If you store that password as plain text in your database, anyone with database access can read it. If the database gets compromised, it can lead to massive data breaches and legal liabilities.
Hashing passwords ensures:
- Security: Even if someone gains access to your database, they won’t see the actual passwords.
- Compliance: Security best practices and data protection laws recommend (or require) password hashing.
- Trust: Users can trust that their data is handled securely.
Laravel 12 Hashing System
Laravel 12 offers a robust hashing system that’s both simple to use and highly secure. The core of Laravel’s hashing system is the Hash facade, which provides static methods for creating and verifying hashed values.
Step 1: Install Laravel 12 (If Not Already Installed)
If you haven’t installed Laravel 12 yet, you can do so using Composer:
composer create-project laravel/laravel laravel-hashing-example
Navigate into the project directory:
cd laravel-hashing-example
Make sure your environment is set up and Laravel is running locally.
Hashing a Password in Laravel 12
Using the Hash Facade
Laravel provides a Hash::make() method to hash passwords. Let’s look at how to use it.
use Illuminate\Support\Facades\Hash;
$password = 'my_secure_password';
$hashedPassword = Hash::make($password);
This line will return a hashed version of the password using the default algorithm (bcrypt).
You can store $hashedPassword in your database when a user registers or updates their password.
Example in a Controller
Let’s say you have a controller handling user registration. Here’s how you might hash the password before saving the user:
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
public function register(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:8|confirmed',
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
return response()->json(['message' => 'User registered successfully.']);
}
This ensures the password is securely hashed before it is stored in the database.
Verifying a Hashed Password
Once a password is hashed and stored, you’ll need to verify it during login.
Use the Hash::check() method:
if (Hash::check($request->password, $user->password)) {
// Passwords match
} else {
// Passwords do not match
}
This method takes the plain-text password and the hashed password, compares them securely, and returns a boolean value.
Hash Driver Configuration
Laravel uses bcrypt as the default driver, but you can configure it in the config/hashing.php file.
Here’s an example of the config file:
return [
'driver' => 'bcrypt',
'bcrypt' => [
'rounds' => 12,
],
'argon' => [
'memory' => 65536,
'threads' => 1,
'time' => 4,
],
];
You can change the driver to argon or argon2id if needed:
'driver' => 'argon2id',
This is useful for applications that require stronger password hashing strategies.
Using Argon2 in Laravel 12
To use Argon2 or Argon2id, you must ensure your PHP version supports it. Laravel will automatically use PHP’s native password hashing functions if the algorithm is available.
$password = Hash::make('my_password'); // uses the algorithm specified in config
No change is needed in your code other than selecting the right driver in config/hashing.php.
When Should You Rehash Passwords?
Laravel includes a method called needsRehash() that can determine if a hashed password needs to be rehashed based on changes in the configuration (e.g., increasing the bcrypt rounds):
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($request->password);
$user->save();
}
This is especially useful if your application is upgrading its hashing strategy over time for stronger security.
Artisan Command for Hashing Passwords
You can also generate a hash directly from the command line using Artisan:
php artisan hash:make
This command will prompt you for a password and return the hashed result. It’s handy for quickly generating secure passwords during development or for admin users.
Storing Hashed Passwords in the Database
Assuming you have a users table, your migration might look like this:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
When storing a password, use the Hash::make() method as discussed. When retrieving and checking the password, use Hash::check().
Password Hashing in Laravel Breeze and Jetstream
If you’re using Laravel Breeze, Jetstream, or Fortify (starter kits for authentication), they already handle password hashing internally. You don’t need to manually hash passwords—these packages use the Hash facade under the hood.
Example from Breeze’s RegisterController:
User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
You can customize this behavior if needed but remember not to store raw passwords under any circumstance.
Common Mistakes to Avoid
Here are a few pitfalls developers might run into:
- Storing raw passwords: Never store a plain-text password.
- Hashing an already hashed password: Always make sure to hash only raw user input, not previously hashed data.
-
Using weak hashing algorithms manually: Avoid using outdated functions like
md5()orsha1(). - Skipping validation: Always validate password length and format before hashing.
-
Not checking for rehashing: If your config changes, use
needsRehash()to ensure older passwords get updated.
Conclusion
Hashing passwords is a non-negotiable aspect of secure web application development. Laravel 12 provides developers with a powerful, secure, and simple-to-use interface for password hashing. Whether you’re building an authentication system from scratch or using Laravel’s built-in starter kits, hashing is seamlessly integrated and highly configurable.
By using the Hash facade, choosing the right algorithm, and implementing best practices, you can ensure that user passwords are safely stored and managed. Always keep security at the forefront of your development process—your users depend on it.
Key Takeaways:
- Use
Hash::make()to hash passwords before saving them. - Use
Hash::check()to verify user input against the stored hash. - Configure your preferred hashing algorithm in
config/hashing.php. - Use
needsRehash()when changing your hashing strategy. - Laravel’s starter kits handle hashing for you—understand how they work for better customization.
Let me know if you’d like this blog formatted for publishing (e.g., with HTML tags or Markdown), or if you want to add code snippets for login/registration forms!

Leave a Reply