Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel migrations: dropping a specific table

Is there any way/laravel-command to drop a specific table from the production server?

like image 608
Noob Coder Avatar asked May 16 '16 08:05

Noob Coder


People also ask

How to migrate a table in Laravel?

You need to specific table migration in Laravel because you don’t want to drop or trunk the existing data within the tables or for some other reasons you just don’t want to play with the database. Create a migration first Open your terminal and run this command. php artisan make:migration create_mrasta_table

Why migration in Laravel is a pain?

Sometimes, migration will be a pain when you have mistakenly added the faulty code and when you run migration then it will create only some of the tables and it will stop creating tables when it encountered an error in specific migration. So let’s see how to solve errors and handle the migration in the Laravel.

How to run specific migration in Laravel using PHP artisan?

To run the specific migration in Laravel, you need to use --path option with the php artisan migrate command. Let’s take a simple example, we have '2019_12_04_131405_create_payments_table.php' migration in the database/migrations directory and we would like to run this migration.

What is up and down method in Laravel migration?

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method. Within both of these methods, you may use the Laravel schema builder to expressively create and modify tables.


1 Answers

Set up a migration.

Run this command to set up a migration:

php artisan make:migration drop_my_table

Then you can structure your migration like this:

<?php

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

class DropMyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // drop the table
        Schema::dropIfExists('my_table');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // create the table
        Schema::create('my_table', function (Blueprint $table) {
            $table->increments('id');
            // .. other columns
            $table->timestamps();
        });
    }
}

You can of course just drop and not check for existence:

Schema::drop('my_table');

Read further in the docs here:

https://laravel.com/docs/5.2/migrations#writing-migrations

You may also have to consider dropping any existing foreign keys/indexes, for example if you wanted to drop a primary key:

public function up()
{
    Schema::table('my_table', function ($table) {
        $table->dropPrimary('my_table_id_primary');
    });

    Schema::dropIfExists('my_table');
}

More in the docs in dropping indexes etc here:

https://laravel.com/docs/5.2/migrations#dropping-indexes

like image 59
haakym Avatar answered Nov 15 '22 10:11

haakym