How to Generate Random Unique String in Laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will show you How to Generate Random Unique String in Laravel? We are know that Laravel provide us several string helper on that way we can use this very easily as like str_limit, str_plural, str_finish, str_singular etc. If we need to generate any unique random string then we can use str_random() helper of Laravel. It is a very simple and we can use this easily.

We can easily generate random string in any version of laravel 6, laravel 7 and laravel 8 by using str helper.

str_random() helper take a one numeric argument and then return number of unique string that we can pass as argument. We can see it’s syntax and example:

Syntax:
Str::random(number);
OR
str_random(number);
Example:
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Str;
  
class GoogleController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function index()
    {
        $randomString = Str::random(30);
        dd($randomString);
    }
}

Output:

RAXY4XmITwkoEfNnZcwBggjbeKfzwD

Read Also : How to Create Custom Blade Directive in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →