Use Str::password() to generate cryptographically secure random passwords with customizable length and character constraints.
Generating random temporary passwords for user onboarding or password resets using random string helpers often produces strings that fail common password complexity rules (e.g. missing numbers or symbols).
Str::password() generates strong, cryptographically secure passwords.
Basic Usage
use Illuminate\Support\Str;
// Generates a secure 32-character password with letters, numbers, and symbols
$password = Str::password();
// Output: "k8#N!x9$mP2@vL5*qR7&wT1^yB4%zC6"
Custom Length and Character Rules
Control password length and character requirements with named parameters:
$tempPassword = Str::password(
length: 16,
letters: true,
numbers: true,
symbols: true,
spaces: false
);
In User Creation Workflows
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($plainTextPassword = Str::password(16)),
]);
$user->notify(new SendTemporaryPasswordNotification($plainTextPassword));
Summary
- Generates cryptographically secure random passwords via PHP's random_int engine.
- Configurable length, symbols, numbers, and spaces.
- Built-in to Laravel's
Strandstr()helper.
Related Tips
View all tips →Mask Sensitive Information with Str::mask()
Use Str::mask() to obfuscate portions of email addresses, phone numbers, and payment details for secure display in user interfaces.
Collapse Consecutive Duplicate Characters with Str::deduplicate()
Use Str::deduplicate() to collapse repeated consecutive spaces, slashes, or characters into a single instance.