How To Add Primary Key In Laravel Migration

  Jan 2024
  ITSolutionsGuides
  Category: Laravel
How To Add Primary Key In Laravel Migration

Welcome To ITSolutionsGuides,

Lets see How To Add Primary Key In Laravel Migration using it solutions guides tutorial. Lets see how the primary key of the laravel migration table are set. By default every migration in the laravel uses the id of the migration table as the primary key . To change the primary key and declare another column as the primary key we should use the primary() function in the laravel migration to add the primary key to another column.So lets see how to add primary key in laravel migration.
Learn to enhance your Laravel database structure by adding primary keys with ease using migrations. Explore step-by-step guidance for seamless implementation. Creating migration using artisan command

Let's Start Coding

Example 1) Using id()

It is the most comman was of declaring id as the primary key in the table , Since the index of the table will be unique. By using the id() in the migration the particular column will be declared as the primary key.

php artisan make:migration create_products_table


database/migrations/2024_01_05_095608_create_products_table.php

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};

Let's Start Coding

Example 2) Using primary()

In laravel declaring another column as the primary we should use the primary() function to declaring the column as the primary key. Since we are declaring another column as the primary we should not use id() .
Since a table should have only one primary key.

php artisan make:migration create_products_table



database/migrations/2024_01_05_095608_create_products_table.php

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->integer('id');
            $table->string('name');
            $table->string('code')->primary();
            $table->text('description');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides