Laravel

How to create a migration in Laravel?


Introduction

Laravel migration is version control of your database so that the developers can change and share the schema of the application’s database. These are somewhat like database backups with each migration file containing operations for modifying the database, creating or altering tables, creating indexes or changing columns. 

Laravel migrations make managing databases easy, sharing work among developers as well as using different environments like, local, staging, and production when developing. Migrations are developed in PHP so they don’t depend on specific databases such as MySQL, PostgreSQL, SQLite etc.

Why is database migration needed?

  • Version Control: They make migration possible where versions of the schema behind the database to be used can be maintained; this also makes possible roll back in this same context.

  • Database Portability: Migrations provide the ability to use and work with multiple database systems simultaneously at the same time, while eliminating the need for having to write specific SQL code in the mainstream or, as in migrations, in PHP.

  • Collaboration: As a result, when in the product development of the team environment, more than one developer is able to come up with the database schema at the same instance without their cropping up of conflicts because of ease that comes along with synchronization with migrations.

  • Automation: They make procedures for developing and changing the structure of a database after the installation process or even when defining new environments.

  • Rollback Support: If the migration settings have an error then the database can easily be rolled back containing the previous version of the schema.

How to create a migration?

For versioning the database schema in Laravel one utilizes something called migrations. Migrations enable you to give the explanation of the format of data in the tables using the PHP codes that might be altered as well as could be made available to other developers.

Here’s a general guide to creating and running a database migration in Laravel:

1. Create a Migration

To create a new migration, run the following Artisan command:

 

php artisan make:migration create_table_name_table

 

 

This will create a new migration file in the directory of database/migrations. Finally, the name of the file will be a timestamp to execute the migrations in the right order.

2. Meaning and forming the schema in migration

In the migration file, there are two methods: up() and down(). The up() method is actually used to say what needs to be done on the database and the down() method should ideally be the reverse of this.

Example of a migration for add column to existing table:

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration {   /**     * Run the migrations.     */   public function up(): void   {       Schema::table('users', function (Blueprint $table) {           if (!Schema::hasColumn('users', 'phone_number')) {               $table->string('phone_number')->nullable();           }       });   }   /**     * Reverse the migrations.     */   public function down(): void   {       Schema::table('users', function (Blueprint $table) {           if (Schema::hasColumn('users', 'phone_number')) {               $table->dropColumn('phone_number');           }       });   } };

 

 

3. Running the Migration

To apply the migration and update your database schema, run:

 

php artisan migrate

 

Run specific migration to apply change in your database schema:

 

php artisan migrate --path = "path of your migration file"

 

Example of a run specific migration:

To add phone number column to users table the command used is 

 

php artisan migrate --path=database/migrations/2024_10_21_071240_add_phone_number_column_to_users_table.php

 

 

4. Rolling Back Migrations

If you need to roll back the last migration, you can use:

 

php artisan migrate:rollback

 

Example of rollback specific migration:

 

php artisan migrate:rollback --path=database/migration s/2024_10_21_071240_add_phone_number_column_to_users_table.php

 

 

This will perform the reversal of the last migration through a method, down() of the migration.

To rollback all migrations:

 

php artisan migrate:reset

 

 

5. Refreshing Migrations

You can also refresh the entire migration by rolling back and running all migrations again:

 

php artisan migrate:refresh 

 

This is useful when working with database schema in development so that you can be able to reset it.

Conclusion

The advantages that developers get after migrations include easy collaborations, ability to support the rollback feature and have consistent and automated updates for the database. Migrations are especially handy for any change since they are always structured and reversible, hence making the database to be easily managed in cases where one is developing independently or when the developer is in a team.

Ready to transform your business with our technology solutions?   Contact Us today to Leverage Our Laravel Expertise. 


Laravel

Related Center Of Excellence