Laravel

How to Generate Custom Unique ID in Laravel

Ravindra Kumar

The unique ID can be important part of any web application because it uniquely identify the record by that particular id and this unique id can be used for many purpose. Some people like to generate unique id as auto incremental and someone like to generate unique id in custom format. This custom id can be collection of numeric and alphabet.

Step 1

To generate unique id in Laravel, You need to install ID Generator package in Laravel command prompt so following is the command to install that package.

composer require haruncpi/laravel-id-generator

Step 2

You can use this ID Generator in controller or model by importing it.

use Haruncpi\LaravelIdGenerator\IdGenerator;
$id = IdGenerator::generate(['table' => 'users','field' => 'property_id', 'length' => 4, 'prefix' =>'PR']);

table is used to give the name of table where you want to generate the Unique Id, field is used to give the column reference for which you want to generate custom unique id. If you don’t give reference column name, it will be applied on default id column.length is used to define the number of digits which you want to display in you custom id format.prefix is used to display something which required to be prefixed.

public function registration_submit(Request $request)
{
	$id = IdGenerator::generate(['table' => 'users','field' => 'property_id', 'length' => 4, 'prefix' =>'PR']);
        
            $hotel_user = new User();
            $hotel_user->property_id = $id;
            $hotel_user->property_name = $request->property_name;
            $hotel_user->email = $request->email_address;
            $hotel_user->password = bcrypt($request->password);
            $hotel_user->status = 0;
            $hotel_user->remember_token = $randToken;
            $res = $hotel_user->save();

}

Variable $id has the custom unique id which is generated by IdGenerator::generate() function and it will be inserted into property_id column.If you want to generate unique id for ID column then make this id folumn fillable into your model and define public $incrementing =false; in your migration file.

How to Run Migration Under a Folder in Laravel

How to exit outer foreach loop in laravel

How to exit outer foreach loop in laravel

Ravindra Kumar

Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.

Suggested Reading

How to Clear Cache in Laravel Without Using Artisan Commands

When working with Laravel, caching plays a key role in improving your application’s performance by storing compiled configurations, routes, views, and other data. However, during development or after making changes, you may need to clear the cache to see your latest updates. Usually, developers use Artisan commands like php artisan cache:clear or php artisan optimize:clear. […]

Upload (restore/import) a MySQL database backup via the command prompt (CLI)

To upload (restore/import) a MySQL database backup via the command prompt (CLI), you can use the mysql command-line utility. Here’s how: You must follow these requirement You must have a MySQL backup file (e.g., backup.sql) MySQL server should be installed and running You should have credentials for the MySQL user If you are working with […]

How to add new column in laravel migration without losing data

To add a new column in a Laravel migration without losing existing data, you can create a new migration file that alters the existing table. Laravel makes this easy using the php artisan make:migration command with the --table option. Step-by-Step Guide: You can generate a new migration php artisan make:migration add_column_name_to_your_table_name_table --table=your_table_name Replace column_name with […]

imagick php extension must be installed to use this driver in Laravel

It means that your Laravel app (or a package like Intervention Image, Spatie Media Library, or Laravel Snappy) is trying to use the Imagick image processing library, but it’s not installed or enabled on your server. If you’re using Ubuntu/Debian (Linux server): sudo apt-get update sudo apt-get install -y php-imagick sudo service apache2 restart # […]

Attempt to read property “id” on null in Laravel

The error message “Attempt to read property ‘id’ on null” in Laravel typically occurs when you’re trying to access the id property of an object that is null. This commonly happens when you’re expecting an object to be returned (such as a database record), but instead, null is returned. You can handle this null value […]

How to check first record in Laravel blade file

Here’s how to check the first iteration in a Blade @foreach loop: You can use Laravel’s built-in $loop variable: @foreach ($all_categories_ as $vals) <li class="nav-item" role="presentation"> <button class="nav-link p-3 border-0 text-start lh-sm w-100 rounded-0 fs-6 fw-medium {{ $loop->first ? 'active' : '' }}" id="pills-navTab{{ $vals->id }}-tab" data-bs-toggle="pill" data-bs-target="#pills-navTab{{ $vals->id }}" type="button" role="tab" aria-controls="pills-navTab{{ $vals->id }}" […]