How can I create a unique random string in laravel

0 votes

Shown below is my attempt to the problem

private function generate_app_code($application_id) { 
        $token = $this->getToken(6, $application_id);
        $code = 'EN'. $token . substr(strftime("%Y", time()),2);

        return $code;
    }

    private function getToken($length, $seed){    
        $token = "";
        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $codeAlphabet.= "0123456789";

        mt_srand($seed);      // Call once. Good since $application_id is unique.

        for($i=0;$i<$length;$i++){
            $token .= $codeAlphabet[mt_rand(0,strlen($codeAlphabet)-1)];
        }
        return $token;
    }

Can the code above do the trick?

Sep 25, 2020 in Laravel by kartik
• 37,520 points
3,640 views

1 answer to this question.

0 votes

Hello @kartik,

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.

Hope it helps!!
Thank you!!

answered Sep 25, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

How can I build a condition based query in Laravel?

Hello @kartik, Try this: $query = DB::table('node'); if ($published == ...READ MORE

answered Sep 28, 2020 in Laravel by Niroj
• 82,840 points
672 views
0 votes
1 answer

How can I create a migration to add a value to an enum in eloquent?

Hello @kartik, Try using this code: public function up() ...READ MORE

answered Nov 12, 2020 in Laravel by Niroj
• 82,840 points
2,532 views
0 votes
1 answer

How can I find the current language in a Laravel view?

Hello @kartik, The cleanest way to know the ...READ MORE

answered Dec 3, 2020 in Laravel by Niroj
• 82,840 points
4,521 views
0 votes
1 answer

How can I obtain a list of all files in a public folder in laravel?

You could create another disk for Storage ...READ MORE

answered Dec 8, 2020 in Laravel by Niroj
• 82,840 points
3,985 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,995 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,694 views
0 votes
1 answer

How can we create a record in Laravel using eloquent?

We need to create a new model ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,840 points
1,211 views
0 votes
1 answer

How can I modify a migration in Laravel?

Hello @kartik, You should create a new migration ...READ MORE

answered Sep 28, 2020 in Laravel by Niroj
• 82,840 points
4,214 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP