You can use :

sha1(time())

Explanation: sha1 is hash function, and most important characteristic of hash function is that they never produce the same hash of different string, so as time() is always unique in theory sha1(time()) will always give you unique string with fixed width.

EDITED:

You can use you function but before giving token you can connect to database and check if token exists, if exists generate new token, if not exists give hin this token. This mechanism will give you unique tokens.

Answer from fico7489 on Stack Overflow
๐ŸŒ
Laravel Daily
laraveldaily.com โ€บ post โ€บ generate-random-strings-laravel-helper-methods
Generate Random Strings with Laravel: Helper Methods
May 22, 2024 - Another option is to combine random_bytes() and bin2hex() PHP functions to generate a 20 character string. ... For a random number, the mt_rand() could be used. The default minimum value is zero, and the maximum value is generated by mt_getrandmax.
Discussions

Is it possible to generate unique and random string easily in Laravel 4? | Laravel.io
The Laravel portal for problem solving, knowledge sharing and community building. More on laravel.io
๐ŸŒ laravel.io
string - Laravel str_random() or custom function? - Stack Overflow
I'm using Laravel 6 version, on that Illuminate\Support\Str::random works well however neither str_random nor Illuminate\Support\Str::str_random works for me using Laravel 6. ... /** * Generate a more truly "random" alpha-numeric string. More on stackoverflow.com
๐ŸŒ stackoverflow.com
php - Generate random number in Laravel - Stack Overflow
I've updated the answer. As for your comments: 1. Sorry, that was true. I was using, like you, the str_random function of Laravel and that function generates a random alpha-numeric string. 2. Sorry, corrected that in the updated answer. 3. I would say its very unlikely that this method generates ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Insert a random number that is unique to the database
Probably a few points. Any reason for not making an order table, then making a ticket_order pivot table, possibly with a quantity of tickets and price tickets were at the time? It would allow you to store timestamps of the order being created, a customer possibly. If all of that isn't really what you want to go down, you could just generate a UUID and store that instead. Change your order_id to a varchar instead of an int. https://github.com/webpatser/laravel-uuid More on reddit.com
๐ŸŒ r/laravel
16
5
February 8, 2020
๐ŸŒ
Medium
medium.com โ€บ @harrisrafto โ€บ enhance-your-string-manipulation-with-str-random-in-laravel-ea3a3ac29f49
Enhance your string manipulation with Str::random() in Laravel | by Harris Raftopoulos | Medium
July 8, 2024 - Letโ€™s dive into how Str::random() can enhance your string manipulation in Laravel projects. The Str::random() function is part of the Illuminate\Support\Str class. It generates a random string of the specified length using a pool of alphanumeric ...
๐ŸŒ
Laravel.io
laravel.io โ€บ forum โ€บ 08-29-2014-is-it-possible-to-generate-unique-and-random-string-easily-in-laravel-4
Is it possible to generate unique and random string easily in Laravel 4? | Laravel.io
The reason I use quickRandom is that it's quicker (as the name suggests) than "secure" random. We aren't using the generated strings for cryptography, so there's no worry. Last updated 3 years ago. ... Sign in to participate in this thread! ... The Laravel portal for problem solving, knowledge sharing and community building.
๐ŸŒ
GitHub
github.com โ€บ webcraft โ€บ laravel-random
GitHub - webcraft/laravel-random: Laravel 5 wrapper around ircmaxell/RandomLib
If no character list is specified, the following list of characters is used: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/ ... // Give the character list 'abcdef': print Random::generateString(32, 'abcdef')."\n"; // One would ...
Starred by 10 users
Forked by 2 users
Languages ย  PHP 100.0% | PHP 100.0%
Find elsewhere
๐ŸŒ
Stillat
stillat.com โ€บ blog โ€บ 2017 โ€บ 12 โ€บ 06 โ€บ laravel-5-string-helpers-generating-random-strings
Laravel 5 String Helpers: Generating Random Strings ยป Stillat
The random helper method in Laravel generates a random string of the specified length. It uses the OpenSSL function openssl_random_pseudo_bytes and requires the OpenSSL extension to be installed.
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ how-to-generate-random-unique-string-in-laravel-5example.html
How to Generate Random Unique String in Laravel? - ItSolutionstuff.com
April 16, 2024 - Laravel provide several string helper that way we can use it easily like str_limit, str_plural, str_finish, str_singular etc. If you need to generate unique random string then you can use str_random() helper of Laravel.
๐ŸŒ
GitHub
github.com โ€บ jorenvh โ€บ Hashify
GitHub - jorenvh/Hashify: Laravel package for generate random (database unique) strings ยท GitHub
private $hashify; public function __construct(Hashify $hashify) { $this->hashify = $hashify; } public function myMethod() { // random string echo $this->hashify->make($minLength, $maxLength); // database unique random string echo $this->has...
Author ย  jorenvh
๐ŸŒ
Code And Deploy
codeanddeploy.com โ€บ home โ€บ blog โ€บ laravel โ€บ laravel str::random() helper function example
Laravel Str::random() Helper Function Example
May 28, 2022 - Str::random(10); echo '<br>'; echo 'Generated random string 2 : ' . Str::random(15); echo '<br>'; echo 'Generated random string 3 : ' . Str::random(30); } } I hope it helps. Thank you for visiting.
๐ŸŒ
Packagist
packagist.org โ€บ packages โ€บ webcraft โ€บ laravel-random
webcraft/laravel-random - Packagist
If no character list is specified, the following list of characters is used: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/ ... // Give the character list 'abcdef': print Random::generateString(32, 'abcdef')."\n"; // One would expect to receive output that only contained those ...
๐ŸŒ
Weblance-online
weblance-online.com โ€บ home โ€บ website โ€บ laravel โ€บ using laravel helper methods to generate random strings
Using Laravel Helper Methods To Generate Random Strings - Weblance Online Solutions
June 19, 2024 - The Laravel framework provides several helper methods for generating random strings and numbers, including str_random() and random(). These methods make it easy to generate random data with just a single line of code, saving developers time ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Laravel: Get a Random String in 5+ Ways - YouTube
There are various helper methods in Laravel/PHP to generate random strings.Faker docs: https://fakerphp.github.io/- - - - -Support the channel by checking ou...
Published ย  July 1, 2024
Top answer
1 of 7
87

str_random (Str::random()) tries to use openssl_random_pseudo_bytes which is a pseudo random number generator optimized for cryptography, not uniqueness. If openssl_random_pseudo_bytes is not available, it falls back to quickRandom():

public static function quickRandom($length = 16)
{
    $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    return substr(str_shuffle(str_repeat($pool, 5)), 0, $length);
}

In my opinion quickRandom code is not reliable for uniqueness nor cryptography.

Yes, having openssl_random_pseudo_bytes and using 32 bytes is almost impossible to see a collision, but it's still possible. If you want to make sure your strings/numbers will be unique (99.99%), you better use a UUID function. This is what I normally use:

/**
 * 
 * Generate v4 UUID
 * 
 * Version 4 UUIDs are pseudo-random.
 */
public static function v4() 
{
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

    // 32 bits for "time_low"
    mt_rand(0, 0xffff), mt_rand(0, 0xffff),

    // 16 bits for "time_mid"
    mt_rand(0, 0xffff),

    // 16 bits for "time_hi_and_version",
    // four most significant bits holds version number 4
    mt_rand(0, 0x0fff) | 0x4000,

    // 16 bits, 8 bits for "clk_seq_hi_res",
    // 8 bits for "clk_seq_low",
    // two most significant bits holds zero and one for variant DCE1.1
    mt_rand(0, 0x3fff) | 0x8000,

    // 48 bits for "node"
    mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}

It generates a VALID RFC 4211 COMPLIANT version 4 UUID.

Check this: https://en.wikipedia.org/wiki/Universally_unique_identifier#Collisions

2 of 7
75

you can use this

use Illuminate\Support\Str;

$random = Str::random(40);
๐ŸŒ
Laravel News
laravel-news.com โ€บ home โ€บ laravel packages โ€บ the random package generates cryptographically secure random values
The Random package generates cryptographically secure random values - Laravel News
April 15, 2024 - $password = Random::dashed(int $length = 25, string $delimiter = '-', int $chunkLength = 5, bool $mixedCase = true): string; Securely shuffle an array, string, or Laravel Collection, optionally preserving the keys.
๐ŸŒ
Medium
medium.com โ€บ @randomstr โ€บ random-string-generation-in-php-and-its-frameworks-bb4b2f1bbce6
Random String Generation in PHP and Its Frameworks | by Random STR | Medium
December 1, 2023 - This function uses random_bytes, ... PHP framework, provides a more straightforward method to generate random strings using the Str helper:...
๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ web development โ€บ laravel โ€บ how can i create a unique random string in...
How can I create a unique random string in laravel | Edureka Community
September 25, 2020 - Shown below is my attempt to the problem private function generate_app_code($application_id) { $token = $this ... } Can the code above do the trick?