Alter existing database table column with Laravel 7 migration

Migration is powerful feature of laravel framework which is handling though it's own cli tool i.e. Artisan. It's a amazing feature to manage for version control of database. Now, we are going to elaborate some steps to alter a column in existing database table.
Step 1: Open Terminal
As we know for running cli command, we need to open a terminal.
Step 2 : Install migrations table
Migration is core feature of laravel but we need to install it by cli command.
php artisan migrate:install
when we run this command, it'll create migration repository in databse with two tables i.e. migrations and failed_jobs which will store all informations about future migrations.
Step 3 : Create migration
After installing migration, we are going generate a empty migration script.
php artisan migrate:make create_products
"create_products" is just a simple name for of understanding of migration script. You can update it too.
This command will create skeleton of migration at path i.e. [ROOT_OF_YOUR_APPLICATION]/database/migrations/ by adding timestamp in file name like 2020_18_02_213439_create_products.php
class Create_Products extends Migration { /** * Make changes to the database. * * @return void */ public function up() { // } /** * Revert the changes to the database. * * @return void */ public function down() { // } }
Step 4: Create Table and add some database
After generating a skeleton of migration, we are going add some script to create a products table in database and insert some test data in products table.
class Create_Products extends Migration { /** * Make changes to the database. * * @return void */ public function up() { Schema::create('products',function($table){ $table->increments('id'); $table->string('name', 20); $table->string('description', 255); $table->string('sku', 64); $table->boolean('available'); $table->timestamps(); }); DB::table('products')->insert(array( 'name' => 'TEST PRODUCT 01', 'string' => 'lorem ipsum text lorem ipsum text', 'sku' => 'PRDCT8761', 'available' => 1 )); } /** * Revert the changes to the database. * * @return void */ public function down() { // }
Step 5 : Run Migration
In this step, we need to run migration for creating "products" table.
php artisan migrate
Now, we have created a table i.e. products in our database.
Note: If you already have existing table which you want alter by a column, you don't need to perform above mentioned Step 3 to Step 5.
Step 6 : Create Migration for alteration of a column
As mentioned above in "Step 3", we need to create a skeleton of migration of alteration of a column.
php artisan migrate:make update_products_by_available_type_change
This will create a skelton file like 2020_18_02_213439_update_products_by_available_type_change.php
Step 7 : Update alter script
class UpdateProductsByAvailableTypeChange extends Migration { /** * Make changes to the database. * * @return void */ public function up() { DB::query("ALTER TABLE `testdb`.`products` CHANGE COLUMN `available` `available` tinyint(1) NOT NULL DEFAULT '1';"); } /** * Revert the changes to the database. * * @return void */ public function down() { // } }
Step 8 : Run Migration
In this step, we need to run migration for adding our alteration script.
php artisan migrate
Now, we are done with alteration of a column in database table.