In this article, we’ll explore the basics of Laravel migrations and how to use them effectively. Migrations are like version control for your database, allowing your team to define and share the application’s database schema definition.
Popular PHP web application framework Laravel offers a variety of features and tools for creating cutting-edge and reliable web apps.
Its built-in support for database migrations, which helps developers to manage database changes and updates in a logical and organized fashion, is one of Laravel’s core features.
In this post, we’ll examine Laravel migration in more detail and discuss how it can improve the way you handle database schema changes.
What is Laravel Migration?
Laravel migration is a feature that allows developers to define and manage database schema changes in a structured and repeatable way. You can use Laravel migration to create, modify, or delete database tables, columns, indexes, and other schema elements using simple PHP code.
Before the go to the deep dive into Laravel migration, let’s understand some common terms of migrations:
- Schema: Laravel migration uses the schema builder to create, modify or delete database tables.
- Artisan: Laravel’s command-line interface (CLI) that allows developers to generate and manage migrations.
- Up and down: Laravel migration has two main methods – up and down. The up method is used to define the changes to be made to the database schema, while the down method specifies how to undo those changes.
- Rollback: Laravel migration allows developers to undo the most recently applied migration using the rollback command.
- Foreign keys: Laravel migration enables the definition of foreign keys and their relationships with other tables in the database.
- Indexes: Laravel migration allows developers to define indexes for columns in their database tables, which helps to speed up queries.
- Constraints: Laravel migration enables the definition of constraints, such as unique or not null, on columns in database tables.
- Migrate: Laravel’s Artisan command to apply all pending migrations to the database.
- Migration status: Laravel migration allows developers to check the status of migrations using the Artisan command migrate:status, which displays a list of all migrations and their current status.
- Seeding: Laravel migration allows developers to seed their database with initial data using seeders, which are classes that define data to be inserted into the database during the migration process.
Creating a Migration in Laravel
The make:migration
Artisan command in Laravel can be used to build a new migration by producing a new migration file in the database/migrations directory. Two methods—up and down—that specify the actions to be taken when the migration is applied or rolled back, respectively, are included in the migration file.
Let’s create a users table using the migration command:
php artisan make:migration create_users_table
This will generate a new migration file with a name like 2023_04_09_000000_create_users_table.php
into database/migrations directory. You can then open the file and define the up and down methods like this:
public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } public function down() { Schema::dropIfExists('users'); }
The up()
method creates a new users table with columns, while the down method drops the table if it exists.
You can then run the migration using the migrate Artisan command:
php artisan migrate
Once the run the above command, It’ll create the users table in your database.
How to update Migration in Laravel
Sometimes, You need to modify an existing migration. You can create a new migration that modifies the schema and runs it after the original migration. Let’s update our users table and add a new column here, The new migration like this:
php artisan make:migration add_role_to_users_table
A new migration file with the name 2023_04_09_000001_add_role_to_users_table.php
will be created as a result. The up and down methods can then be defined by opening the file as follows:
public function up() { Schema::table('users', function (Blueprint $table) { $table->string('role')->default('user'); }); } public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('role'); }); }
The up()
function adds a new role column with the default value of user to the users table, whereas the down()
method removes the column from the table.