Home > Software design >  Where could I update Laravel Migration files Foreign key scripts
Where could I update Laravel Migration files Foreign key scripts

Time:02-12

working with Laravel project and need update titles table new columns like employee_id and create new migration command php artisan make:migration add_employee_id_to_titles_table --table=titles and create new migration file like add_employee_id_to_titles_tables.php now I need add following foreign key relationship with new columns and the titles table

 Schema::table('titles', function ($table){
             $table->foreign('employee_id')->references('id')->on('titles')->onDelete('cascade');
        });

my problem is in witch migration files titles or new add_employee_id_to_titles_tables should I add this scripts?

CodePudding user response:

When you run php artisan migrate it checks your database and migrations table and if there is new migration that didn't save it will run. So you have to write the scripts in a file that has not been executed

CodePudding user response:

First thing why your employee_id reference to the same table title ID. it should reference to either employee table or whatever you're getting this employee_id from.

In order to update your existing Title table you need to create a new migration just like you said php artisan make:migration add_employee_id_to_titles_table --table=titles and than

Schema::table('titles', function ($table){
    $table->unsignedBigInteger('employee_id');
    $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
});
  • Related