Set non primary column as Auto Increment in Laravel

Viewed 27

This is my migration file code

Schema::create('hierarchies', function (Blueprint $table) {
    $table->integer('id');
    $table->integer('hierarchy_id');
    $table->timestamps();
});

I want my ID column will be auto increment without primary and hierarchy_id will be my primary key. How to do that?

1 Answers

After a quick look through the Laravel Migrations manual pages I think thi sshoudl do waht you want, the thing to remember is you need to make the id column unique for it to still be allowed to be a Auto Increment.

Schema::create('hierarchies', function (Blueprint $table) {
    $table->integer('id')->unique();
    $table->integer('hierarchy_id')->autoIncrement();
    $table->timestamps();
    $table->primary('hierarchy_id');
});
Related