Is there any way/laravel-command to drop a specific table from the production server?
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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With