Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to delete column in laravel migration ?

How to delete column in laravel migration ?

Aman Jain, February 17, 2022February 26, 2024

Deleting column to existing table are easy as creating a new table and adding columns to it. To delete column in laravel migration to existing table we can use the same method we used in create migrations . 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 delete a column status 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 delete column in laravel migration and php artisan migrate command to modify the table.

I am assuming that you have already table movies in database and now we are deleting column. also you can read How to add column in laravel migration ?

Let’s understand the delete column in laravel migration in laravel 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 DeleteStatusMoviesTable

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

Output: 
Created Migration: 2022_02_17_172323_delete_status_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 delete column to 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 DeleteStatusMoviesTable 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 DeleteStatusMoviesTable and it extends Migration class. DeleteStatusMoviesTable 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 delete the column using dropColumn method from our table before updating the database.

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

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_status_movies_table
Migrated:  2022_02_17_172323_delete_status_movies_table (27.20ms)

Screenshot Before:

laravel migration  phpmyadmin
laravel migration phpmyadmin

Screenshot After:

laravel migration phpmyadmin
laravel migration phpmyadmin

Delete foreign key before deleting column

Sometime we create foreign keys to create the relation in database so if we delete the column without deleting the relation then it produce error so to eliminate this we can delete foreign key first using dropForeign method as below

 public function up()
 {
        Schema::table('movies', function (Blueprint $table) {
           
           $table->dropForeign('statusFk');
           $table->dropColumn('status');
            
        });
 }
 

Also Read : How to add column in laravel migration ?

Related

Php Laravel deletelaravelmigrationphp

Post navigation

Previous post
Next post

Related Posts

Php How to use limit query in Laravel Eloquent

How to use limit query in Laravel eloquent query with example

February 5, 2022October 4, 2022

Limit clause is used to restrict the number of rows return in executed query. It’s quite useful when working with large data set since querying the large data creates performance issues so to overcome with the large dataset issue we use limit clause in a query. Laravel Limit clause accepts…

Read More
Php Laravel Auto Load Infinite Scroll pagination with search

Laravel Auto Load Infinite Scroll pagination with search

May 8, 2022May 8, 2022

In this tutorial we will learn Auto Load Infinite Scroll pagination with search in Laravel using Ajax jQuery. Laravel provides its own library to build the pagination html, which we can easily use in our html page using $model->links() method and $model->paginate() method to make a long list into pagination.In…

Read More
Php How to change data type of column in laravel migration

How to change data type of column in laravel migration ?

February 20, 2022November 5, 2023

In this blog post we will learn to change data type of column in laravel migration. Laravel covers most of the migration features like add, delete, indexing etc. but to modify the table like renaming column, change data type column to existing table laravel uses a separate package doctrine/dbal. 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

  • June 2025
  • 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

  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version