Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to drop foreign key in laravel migration

How to drop foreign key in laravel migration ?

Aman Jain, September 28, 2022March 16, 2024

Drop foreign key to existing table are easy as creating a foreign key and adding columns to it. In laravel migration we can drop foreign key in laravel migration to existing table using dropForeign method of migration class. Major difference between creating new table and updating new table is Schema::create and Schema::table In this tutorial we will create a new migration file and then will drop foreign key column user_id to the existing table .

I hope you know about How to make database connection in Laravel 8 ?

We will use the php artisan command to generate the drop foreign key in laravel migration and php artisan migrate command to modify the table.

I am assuming that you have already table movies and user_id column with foreign key in database and now we are deleting foreign key.

Also you can read How to add column in laravel migration ?

Steps to drop foreign key in laravel migration

Let’s understand the drop foreign key in laravel migration step by step

Step 1 : Generate migration file

To generate the migration file we will use the laravel artisan command so open the terminal in project and run below command

php artisan make:migration DeleteForeignUserIdMoviesTable

Above command will create a migration file in folder database/migrations

Output: 
Created Migration: 2022_02_17_172323_delete_foreign_user_id_movies_table

Step 2 : Open generated migration file and update

In the last step we created a migration file using the artisan command and now we wanted to drop foreign keyto schema of movies table.

so let’s open the file and start editing

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class DeleteForeignUserIdMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

In the file there is a class named as DeleteForeignUserIdMoviesTable and it extends Migration class. DeleteForeignUserIdMoviesTable class contains two methods one is up and other one is down.

Up is used to update the database scheme and down method is used to rollback the changes of this migration. As you can see we are creating table movies in up method and dropping the table in down method.

let’s add remove the foreign key using dropForeign method from our table before updating the database.

 public function up()
 {
        Schema::table('movies', function (Blueprint $table) {
            
            $table->dropForeign(['user_id', 'another_column']);
        });
 }
 public function down()
 {
        Schema::table('movies', function (Blueprint $table) {
              $table->integer('user_id');
        });
 }

Here we used Schema::table to update the table.

Step 3 : Run Migration

In this step we will execute our migration in database using below command

php artisan migrate

This will create table in database and the output

Output: 
Migrating: 2022_02_17_172323_delete_foreign_user_id_movies_table
Migrated:  2022_02_17_172323_delete_foreign_user_id_movies_table (27.20ms)

Screenshot After:

laravel migration phpmyadmin
laravel migration phpmyadmin

Also Read : How to add column in laravel migration ?

Related

Php Laravel columndeleteforeignlaravelmigrationphp

Post navigation

Previous post
Next post

Related Posts

Php How to Set Default Null Value in Laravel Migration

How to Set Default Null Value in Laravel Migration?

August 18, 2022March 16, 2024

To set default null value in laravel migration we use nullable method of schema class. As we learnt about How to Add Default Value to Column in Laravel Migration? in our last article but sometimes we want to set null value to column instead of any number or string. Null…

Read More
Php Laravel Custom Facade with example

How to create a custom facade with example in Laravel 8 ?

December 22, 2021January 10, 2022

In Laravel there several facades to use like DB, URL, Validator ,Request etc. Facades are used to use the class methods statically using the application service container. In this article we will learn to create our own facades with service container. Let’s start with step by step For a better…

Read More
Php Use Soft Delete to Temporary (Trash) Delete the Records in Laravel

Use Soft Delete to Temporary (Trash) Delete the Records in Laravel 9 ?

June 14, 2022June 14, 2022

Soft delete in laravel is use to hide the record from users by keeping the data in the database. In laravel we use Soft Delete to Temporary (Trash) Delete the Records. There is many purpose of soft delete like deleting the user for admin but keep all the information in…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version