Laravel Str::random() function Example

Morris Muriuki Muthigani
78
0
laravel

12/13/2022 (1 year ago)


In Laravel, the Str::random() function is used to generate a random string of characters. This function is part of the Laravel Str class, which provides a variety of utility methods for working with strings.

Here is an example of how to use the Str::random() function to generate a random string of characters in Laravel:

use Illuminate\Support\Str; $randomString = Str::random(16);

In this example, the Str::random() function is used to generate a random string of 16 characters. The string that is generated will contain a mix of letters, numbers, and special characters.

You can also specify the type of characters that should be included in the generated string by passing an additional argument to the Str::random() function. For example, the following code generates a random string of 16 characters that only contains numbers:

use Illuminate\Support\Str; $randomString = Str::random(16, '0123456789');

The Str::random() function is a useful tool for generating random strings of characters in Laravel applications. It can be used in various situations where a unique, random string is needed, such as for generating passwords or tokens.

Discussion (0)

Recent Blogs

Our Recent Blogs