Before (Post Model)
- id
- title
- slug (column to be removed)
- summary (column to be removed)
- content
- image
After (Post Model)
- id
- title
- content
- image
Create the Migration
To remove the columns you have to run the make migration command but instruct it as removing instead of creating a new migration. Make use of the "--table=your-table-name" flag and specify the table that you want to remove the columns.
# for multiple columns like the scenario php artisan make:migration remove_slug_and_summary_columns_from_posts_table --table=posts # for single column you can explicitly mention it php artisan make:migration remove_slug_from_posts_table --table=posts
Created Migration: 2021_06_14_210156_remove_slug_and_summary_columns_from_posts_table
Remove the Columns
The command will create a new migration class called RemoveSlugAndSummaryColumnsFromPostsTable and here define the columns to remove from the migration in the "up" method.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class RemoveSlugAndSummaryColumnsFromPostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('posts', function (Blueprint $table) { $table->dropColumn(['slug', 'summary']); }); } }
$table->dropColumn('slug');
Run the Migration
Finally, run the migration command and the 2 columns will now be removed from the table.
php artisan migrate