How to create pivot table in laravel using migration

Answers 4
  • Create pivot table using foreignId() method in Laravel 7, 8

    --PATH database\migrations\<yourmigrationfile>.php
    //Migration command to create migration file
    php artisan make:migration create_project_user_table --create=project_user
    
    //Migration file code under - database\migrations\<yourmigrationfile>.php
    public function up(){
        Schema::create('project_user', function (Blueprint $table) {
            $table->foreignId('project_id')->constrained();
            $table->foreignId('user_id')->constrained();
        });
    }
    
    0

    Laravel migrations helps to create a Pivot table with proper columns and foreign key relations to both the users and projects table. Basically the pivot table is the intermediate table between two tables which are connected with relationships using the Many to Many relationship method. The column with the constrained method, in the child table (pivot table) will always reference to the id column of the parent table.

    You have to run - php artisan make:migration create_project_user_table --create --table=project_user to create the pivot table migration file after that you can copy/paste code in this migration file and run migrate command to create pivot table in your database.

    Pivot table names are listed in alphabetical order and should be singular like (project_user). If you want to create a model for the pivot table then extends Pivot instead of model. 

  • Create pivot table with foreignId() method in Laravel 7,8 with timestamps

    --PATH database\migrations\<yourmigrationfile>.php
    //Migration command to create migration file
    php artisan make:migration create_project_user_table --create=project_user
    
    //Migration file code under - database\migrations\<yourmigrationfile>.php
    public function up(){
        Schema::create('project_user', function (Blueprint $table) {
            $table->foreignId('project_id')->constrained();
            $table->foreignId('user_id')->constrained();
            $table->timestamps();
        });
    }
    
    0

    You can add created_at and updated_at columns in the table using $table->timestamps(); in your migration file. 

  • Create pivot table in laravel with multiple columns

    --PATH database\migrations\<yourmigrationfile>.php
    //Migration command to create migration file
    php artisan make:migration create_project_user_table --create=project_user
    
    //Migration file code under - database\migrations\<yourmigrationfile>.php
    public function up(){
        Schema::create('project_user', function (Blueprint $table) {
            $table->foreignId('project_id')->constrained();
            $table->foreignId('user_id')->constrained();
            $table->boolean('is_manager')->default(false);
            $table->timestamps();
        });
    }
    
    0

    You can add multiple columns in the laravel pivot table using migration. This code snippet adds is_manager columns in the pivot table with default value false and adds created_at and updated_at fields using timestamps method.

  • Pivot table with unsigned() , index() , primary() and onDelete(‘cascade’) method

    --PATH database\migrations\<yourmigrationfile>.php
    //Migration command to create migration file
    php artisan make:migration create_role_user_table --create=role_user
    
    //Migration file code under - database\migrations\<yourmigrationfile>.php
    public function up()
    {
        Schema::create('role_user', function (Blueprint $table) {
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->integer('role_id')->unsigned()->index();
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
            $table->primary(['user_id', 'role_id']);
        });
    }
    
    0

    This code snippet add the foreign key user_id and role_id in table with onDelete('cascade') method which automatically deletes the related child table record (pivot table record) on delete the parent table record(users table , roles table). LIke if you delete the user with id 1 then it will delete all the records from pivot table (role_user) where user_id is 1. The migration will also set both role_id and user_id columns as the primary keys so there cannot be duplicates with both the same user_id and role_id.

  • Back to code snippet queries related laravel