PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to change the foreign key reference table with Migration

 April 19, 2022     laravel, laravel-8, laravel-migrations, mysql, php     No comments   

Issue

I had migrated a migration in Laravel which was like this:

$table->unsignedBigInteger('rel_seller_id')->nullable();
$table->foreign('rel_seller_id')->references('id')->on('seller');

Now I need to change the seller table to sellers table.

But don't know how to do this with Migration!

So if you know, let me know please...


Solution

Just create a new migration.

...
public function up()
{
    Schema::table('table_name', function (Blueprint $table) {
        $table->dropForeign(['rel_seller_id']);

        $table->foreign('rel_seller_id')
            ->references('id')
            ->on('sellers');
    });
}
...

It will drop old foreign key and create a new one.



Answered By - S N Sharma
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing