When building web applications, one of the most crucial tasks is managing your database. Laravel makes this easier and more structured with migrations. If you’ve ever struggled with manually updating tables, sharing database changes across team members, or maintaining database consistency across environments, migrations are the solution.
In this blog, we’ll explore what migrations are, why they are important, and how to use them effectively in Laravel.
What are Migrations in Laravel?
Migrations are like version control for your database. They allow you to define your database structure in PHP code, instead of manually running SQL queries. This makes it easy to:
- Create new tables
- Modify existing tables
- Add or remove columns
- Manage relationships
Essentially, migrations are PHP classes that define how your database should be structured and can be shared with your team or deployed to different environments.
Why Use Migrations?
Migrations solve several problems:
- Version Control for Database
Just like Git tracks changes in your code, migrations track changes in your database schema. - Team Collaboration
When working in a team, migrations allow everyone to stay in sync. One developer can create a migration, push it to the repository, and other developers can run it locally. - Environment Consistency
When deploying your app to production, you don’t need to manually create tables. Running migrations ensures your production database matches your development environment. - Rollback Capability
Migrations allow you to undo changes if something goes wrong.
How Migrations Work
A migration is basically a PHP class located in the database/migrations folder. Each migration contains two main methods:
up()– Defines the changes you want to apply to the database (creating tables, adding columns, etc.).down()– Defines how to revert the changes made in theup()method (dropping tables, removing columns, etc.).
Example:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStudentsTable extends Migration
{
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('students');
}
}
In this example:
- The
up()method creates astudentstable withid,name,email, and timestamp columns. - The
down()method drops the table, effectively undoing the migration.
Creating Migrations
To create a migration, use the Artisan command:
php artisan make:migration create_students_table
This will generate a migration file in the database/migrations folder with a timestamp in the filename.
You can also create a migration for modifying a table:
php artisan make:migration add_age_to_students_table --table=students
Running Migrations
Once a migration is created, you can run it using:
php artisan migrate
This will execute all pending migrations and update your database schema.
To rollback the last batch of migrations:
php artisan migrate:rollback
To reset all migrations:
php artisan migrate:reset
To refresh all migrations (rollback and re-run):
php artisan migrate:refresh
Common Migration Methods
Laravel provides a rich set of methods to define columns:
| Method | Description |
|---|---|
$table->id(); | Auto-increment primary key |
$table->string('name'); | VARCHAR column |
$table->text('bio'); | TEXT column |
$table->integer('age'); | INTEGER column |
$table->boolean('active'); | BOOLEAN column |
$table->timestamps(); | Adds created_at and updated_at |
$table->softDeletes(); | Adds deleted_at for soft deletes |
Tips for Using Migrations
- Always use migrations for schema changes instead of manual SQL queries.
- Group related changes in a single migration when possible.
- Use descriptive names for migrations (e.g.,
create_orders_table,add_status_to_users). - Test migrations locally before deploying to production.
- Use
foreignId()for relationships instead of manually adding foreign keys.
Conclusion
Migrations are an essential part of modern Laravel development. They make your database version-controlled, collaborative, and maintainable, saving you from countless headaches. By using migrations consistently, you ensure your application is scalable, robust, and easy to maintain across multiple environments.
