How to Update Table struture using migration – Laravel 8

In Laravel with the use of migration, you can create tables in the database.

Using this you can also update the existing table structure.

In this tutorial, I show you 2 ways to update table structure using migration in Laravel 8.

How to Update Table struture using migration - Laravel 8


Contents

  1. Database Configuration
  2. Create Table
  3. Refresh Migration
  4. Single Table
  5. Conclusion

1. Database Configuration

Open .env file.

Specify the host, database name, username, and password.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tutorial
DB_USERNAME=root
DB_PASSWORD=

2. Create Table

  • Create a new table Employees using migration and add some records.
php artisan make:migration create_employees_table
  • Now, navigate to database/migration/ folder from the project root.
  • Find a PHP file that ends with create_employees_table and open it.
  • Define the table structure in the up() method.
public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->id();
        $table->string('emp_name');
        $table->string('email');
        $table->string('gender');
        $table->smallInteger('active');
        $table->timestamps();
    });
}
  • Run the migration –
php artisan migrate
  • The table is been created and add some records in it.

Laravel table with data


3. Refresh Migration

  • Navigate to database/migration/ folder from the project root.
  • Open table migration file which you want to edit. Here, I am opening employees table migration.
  • Update the table structure in the up() method.
public function up()
{
      Schema::create('employees', function (Blueprint $table) {
             $table->id();
             $table->string('emp_name',80);
             $table->string('email',80);
             $table->string('gender',10); 
             $table->smallInteger('status');
             $table->timestamps();
      });
}
  • Here, I did the following changes –
    • Set the string datatypes field length.
    • Added a new column status, and
    • Delete active column.
  • Refresh the migration –
php artisan migrate:refresh

NOTE – Above command recreates the whole database and delete all data.

Laravel table after migration:refresh execution

  • Or, you can re-migrate a specific number of migrations from last –
php artisan migrate:refresh --step=2
  • The above command only roll back and re-migrate the last two migrations.

4. Single Table

Require doctrine/dbal package to modify existing columns –

composer require doctrine/dbal

Create migration –

php artisan make:migration update_and_addstatus_to_employees_table
  • Now, navigate to database/migration/ folder from the project root.
  • Find a PHP file that ends with update_and_addstatus_to_employees_table and open it.
  • Define the table structure in the up() method.
public function up()
{
       Schema::table('employees', function (Blueprint $table) {
               $table->renameColumn('emp_name', 'employee_name');// Renaming "emp_name" to "employee_name"
               $table->string('gender',10)->change(); // Change Datatype length
               $table->dropColumn('active'); // Remove "active" field
               $table->smallInteger('status')->after('email'); // Add "status" column
       });
}
  • Here, I did the following changes –
    • Rename emp_name column name to employee_name.
    • Changed gender column Datatype length.
    • Delete active column.
    • Add a new status column.
  • In down() method –
public function down()
{
        Schema::table('employees', function (Blueprint $table) {
               $table->renameColumn('employee_name', 'emp_name');
               $table->string('gender')->change(); 
               $table->smallInteger('active');
               $table->dropColumn('status');
        });
}
  • Run the migration –
php artisan migrate
  • Table structure will be updated without losing data.



5. Conclusion

You can use any of the above methods according to your requirement.

If you want to alter table structure without losing data then use the second method.

Only use migrate:refresh method when you want to recreate your whole database or use --step to only re-migrate a specific number of migrations.

If you found this tutorial helpful then don't forget to share.

Leave a Comment