ItSolutionStuff.com

How to Generate Random Unique String in Laravel?

By Hardik Savani • April 16, 2024
Laravel

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. It is very simple and you can use easily.

you can easily generate random string in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version using str helper.

str_random() helper take one numeric argument and return number of unique string that you pass as argument. you 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

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Add Google Map in Laravel?

Read Now →

Laravel Group By with Max Value Query Example

Read Now →

Laravel Carbon Subtract Hours Example

Read Now →

Laravel Firebase Push Notification Tutorial

Read Now →

Laravel Model Events Tutorial

Read Now →

Laravel Eloquent take() and skip() Query Example

Read Now →

Laravel Unique Validation on Update Example

Read Now →

Special Characters Not Allowed Validation in Laravel

Read Now →

How to Change Table Name using Laravel Migration?

Read Now →

How to Check if String Contains Specific Word in Laravel?

Read Now →

How to Generate Route with Query String in Laravel?

Read Now →

How to Get Query Strings Value in Laravel?

Read Now →

Mysql procedure with pagination in laravel?

Read Now →