Here we explain how to create primary key in laravel. laravel primary key migration. Create a string column to primary key.
Table primary key is unique column of a table we can create primary key both type int and string.
Create a new migration
php artisan make:migration create_orders_table
Add auto increment primary key
By default we create primary column name id and its auto increment column of unsigned biginteger type.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateOrdersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('orders', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('orders'); } };
Make a string column to primary key
You can also create string type column to primary key of table see in example.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateOrdersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('orders', function (Blueprint $table) { $table->string('order_number')->primary(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('orders'); } };