How to add foreign key in Laravel migration?

Member

by aubrey , in category: PHP Frameworks , 2 years ago

How to add foreign key in Laravel migration?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by elisha_langworth , 2 years ago

@aubrey 

To add a foreign key constraint to a column in a Laravel migration, you can use the foreign method of the Blueprint class within the up method of your migration file.


Here's an example of how you can do this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class AddForeignKeyToUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->foreign('country_id')
                ->references('id')->on('countries')
                ->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropForeign('users_country_id_foreign');
        });
    }
}


This example adds a foreign key constraint to the country_id column of the users table that references the id column of the countries table. The onDelete method specifies the action to be taken when a record in the countries table is deleted. In this case, the action is cascade, which means that any records in the users table that reference the deleted record will also be deleted.


You can then run the migration using the php artisan migrate command.

Member

by darion , a year ago

@aubrey 

To add a foreign key in a Laravel migration, you can use the foreign method provided by the Schema facade. Here's an example of how to do it:

  1. Create a new migration using the make:migration Artisan command. For example, to create a migration for adding a foreign key to a users table, run the following command: php artisan make:migration add_foreign_key_to_users_table --table=users
  2. Open the newly created migration file database/migrations/yyyy_mm_dd_******_add_foreign_key_to_users_table.php and add the desired foreign key definition inside the up method. For example, to add a foreign key to a posts table with a column named user_id, you can use the following syntax: public function up() { Schema::table('users', function (Blueprint $table) { $table->foreign('user_id')->references('id')->on('posts'); }); } In this example, 'user_id' is the column in the users table that will reference the 'id' column in the posts table.
  3. Inside the same migration file, add the code to drop the foreign key in the down method: public function down() { Schema::table('users', function (Blueprint $table) { $table->dropForeign(['user_id']); }); } This will allow you to roll back the migration if needed.
  4. Run the migration using the migrate Artisan command: php artisan migrate The foreign key will be added to the specified table in the database.


Note: Make sure you have created the necessary tables that the foreign key references before running the migration.