Hi Artisan,
This tutorial will give you example of drop column if exists laravel migration. i would like to show you drop column laravel migration. let’s discuss about remove field migration laravel. if you want to see example of laravel migration remove column then you are a right place.
I will give you some example that way you can easily remove column using migration. let's see bellow example that will help you.
1) Remove Column using Migration
2) Remove Multiple Column using Migration
3) Remove Column If Exists using Migration
1) Remove Column using Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangePostsTableColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('body');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
2) Remove Multiple Column using Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangePostsTableColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn(['body', 'title']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
3) Remove Column If Exists using Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangePostsTableColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasColumn('posts', 'body')){
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('body');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
I hope it can help you...
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example