Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Yasser Elgammal
Yasser Elgammal

Posted on • Edited on

     

Generate Random Password in Laravel

In Laravel, you can generate a random password using thepassword() helper function provided by theIlluminate\Support\Str class.

This function generates a secure password that is 32 characters long by default.

To use this function, you need to import theIlluminate\Support\Str class at the top of your PHP file:

useIlluminate\Support\Str;
Enter fullscreen modeExit fullscreen mode

Once you have imported theStr class, you can call thepassword() method to generate a random password:

$password=Str::password();
Enter fullscreen modeExit fullscreen mode

By default, the generated password will be 32 characters long and will include letters, numbers, and symbols. However, you can customize the behavior of thepassword() function by passing in up to five parameters:

  1. Length: This parameter specifies the length of the password you want to generate. By default, the length is set to 32 characters.

  2. Letters: This parameter determines whether or not to include letters in the generated password. By default, letters are included.

  3. Numbers: This parameter determines whether or not to include numbers in the generated password. By default, numbers are included.

  4. Symbols: This parameter determines whether or not to include symbols in the generated password. By default, symbols are included.

  5. Spaces: This parameter determines whether or not to include spaces in the generated password. By default, spaces are not included.

You can customize the behavior of thepassword() function by passing in the desired values for each of these parameters.

For example, if you want to generate a password that is 16 characters long and only includes letters and numbers, you can call thepassword() function like this:

$password=Str::password(16,true,true,false,false);
Enter fullscreen modeExit fullscreen mode

This will generate a random password that is 16 characters long and includes only letters and numbers.

Function accept first parameter as Integer and other parameters as boolean Which mean you can use true or false to include it to your returned result.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
dipanjan profile image
Dipanjan Ghosal
  • Joined

Thanks! This is exactly what I was looking for!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software engineer @ AAIT
  • Location
    Egypt, Damietta
  • Work
    Software Engineer | Backend Developer | PHP | Laravel | Fintech
  • Joined

More fromYasser Elgammal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp