How to add Delete cascade to existing column in Laravel 5, without remove column?

How to add Delete cascade to existing column in Laravel 5, without remove column?

In this post we will give you information about How to add Delete cascade to existing column in Laravel 5, without remove column?. Hear we will give you detail about How to add Delete cascade to existing column in Laravel 5, without remove column?And how to use it also give you demo for it if it is necessary.

Whenever you are making table using migration with foreign key. like i give you example as under and you forgot to set delete cascade on table then how can you add delete cascade in existing table. so let’s see your migration :

public function up()

{

Schema::create('locations', function (Blueprint $table) {

$table->increments('id');

$table->integer('id_option')->unsigned();

$table->foreign('id_option')->references('id')->on('options');

$table->decimal('lng',11,8);

$table->string('streetAddress1');

$table->decimal('lat',11,8);

$table->timestamps();

});

}

and you will run this migration, but you forgot to give delete cascade on “options” table,i mean you forgot to give like this :

$table->integer('id_option')->unsigned();

$table->foreign('id_option')->references('id')->on('options')->onDelete('cascade');

In “locations” table, you had added lots of records and now you want to implement delete cascade. So, we can give delete cascade without remove any column using DB::statement(), i give you example of this :

public function up()

{

DB::statement("ALTER TABLE locations ADD CONSTRAINT FK_locations FOREIGN KEY (id_option) REFERENCES options(id) ON DELETE CASCADE;");

}

Try this………

Hope this code and post will helped you for implement How to add Delete cascade to existing column in Laravel 5, without remove column?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

82  +    =  87

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US