Is it possible to auto-increment any number other than id
when doing laravel migration?
I want to auto auto-increment increment unique_id
on my migration and not default ID, how do I do it?
Schema::create('lims_codes', function (Blueprint $table) {
$table->id();
$table->increments('unique_id')->startingValue(000001);
$table->timestamps();
});
My Answer:
No, it is not possible to use the `increments` method to auto-increment a number other than the default `id` column in Laravel migration. The `increments` method is specifically for creating an auto-incrementing primary key column.
If you want to auto-increment a custom column like `unique_id`, you can achieve this by manually setting the starting value in your migration file. Here is an example of how you can do this:
php
Schema::create('lims_codes', function (Blueprint $table) {
$table->id();
$table->integer('unique_id')->unsigned()->unique();
$table->timestamps();
});
DB::statement('ALTER TABLE lims_codes AUTO_INCREMENT = 1000001;');
In this example, we first create a `unique_id` column as an integer and set it as unique. Then, we use a raw SQL statement to set the starting value of the `unique_id` column to 1000001. This will ensure that the `unique_id` column auto-increments starting from the specified value.
Rate this post
3 of 5 based on 1456 votesComments