How do you create and run migrations in Laravel?
In Laravel, migrations are a way to manage your database schema. They allow you to create and modify database tables and fields programmatically using PHP code, which is particularly useful for version control and team collaboration.
Here’s how to create and run migrations in Laravel:
1. Creating a Migration
You can create a migration using the artisan
command. For example, to create a migration for a users
table:
php artisan make:migration create_users_table
This command will generate a migration file in the database/migrations
directory with a timestamped filename.
2. Editing the Migration
Once the migration is created, you can define the structure of your table within the up
method. Laravel uses the Schema builder to define the structure of the table.
Example of a migration file:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id(); // Creates an auto-incrementing primary key
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps(); // Creates created_at and updated_at columns
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
- up method: Defines the actions to be performed when the migration is run (usually creating or modifying a table).
- down method: Defines the actions to reverse the migration (typically dropping the table or reverting modifications).
3. Running Migrations
To run all the pending migrations, you use the following command:
php artisan migrate
This will execute all migrations that haven't been run yet and apply the changes to your database.
4. Rolling Back Migrations
If you want to revert the last batch of migrations, you can run:
php artisan migrate:rollback
This will undo the last migration. If you want to rollback all migrations, you can use:
php artisan migrate:reset
Or, to rollback and rerun all migrations:
php artisan migrate:refresh
5. Seeding Data (Optional)
After running migrations, you might also want to seed your database with some default data. You can do that by creating and running seeders using:
php artisan db:seed
Example Workflow:
-
Create a migration:
php artisan make:migration create_posts_table
-
Edit the migration to define the structure of the
posts
table. -
Run the migration to apply the changes to your database:
php artisan migrate
-
If needed, roll back the migration:
php artisan migrate:rollback
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.