create a unique random string in laravel

Generate a unique random string in laravel 8

In this tutorial, I will give you an example of “Generate Random Unique String in Laravel”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

Unique String is mostly used to manage Products, Orders, and Invoices for identifying using his random id on bills or invoices in the application.

Related Article : Generate unique Order numbers in Laravel.

First, what we’re doing here, This is the example :

Laravel provides multiple string helpers, We use substr(md5(mt_rand()) helper of Laravel. It is very simple and we can set the limit of string easily.


substr(md5(mt_rand()), 0, 5);


random string generate in laravel

Generating Migration

At first, we create a new migration file for “unique_string_examples” table, In your cmd terminal hit the given below command

 php artisan make:migration create_unique_string_examples_table

Migration Structure


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUniqueStringExamples extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('unique_string_examples', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

Run Migration

After defining columns and data types For migrating in the cmd terminal hit the given below command.

 php artisan migrate
unique string in laravel

Create a Model

php artisan make:model UniqueStringExample



namespace App;

use Illuminate\Database\Eloquent\Model;

class UniqueStringExample extends Model
    protected $table = 'unique_string_examples';

Create Controller

php artisan make:controller TestController


use App\Http\Controllers\TestController;

Route::get('/add', [TestController::class,'add'])->name('add.string');



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\UniqueStringExample;

class TestController extends Controller

  #Generate random string in Laravel
  public function add(){
    return view('randomstringexample');

  public function store(Request $request){
    $data = new UniqueStringExample();
    $data->name = $request->name;
    $data->email = $request->email;
    $data->contact = $request->contact;
    $data->address = $request->address;
    $data->unique_string = substr(md5(mt_rand()), 0, 5);


<div class="container">
    <div class="row">
    <div class="col-md-8 offset-2 mt-5">
    <div class="card">
    <div class="card-header bg-info">
    <h6 class="text-white">How to Generate Unique String | Laravel</h6>
    <div class="card-body">
    <form method="post" action="{{route('store.string')}}">
    <div class="form-group">
    <input type="text" name="name" class="form-control"/>
    <div class="form-group">
    <input type="email" name="email" class="form-control"/>
    <div class="form-group">
    <input type="text" name="contact" class="form-control"/>
    <div class="form-group">
    <input type="text" name="address" class="form-control"/>
    <div class="form-group text-center">
    <button type="submit" class="btn btn-info">Submit</button>
random string helper in laravel

Now, you can see the output in the blade file, we have successfully Generate Random Unique String in Laravel using PHP in the Laravel application.

unique random string in Laravel

In this article, we learned “How to create a unique random string in Laravel”, I hope this article will help you with your application Project.

Also Read: Limit the length of a string in Blade file Laravel.