0
0
Laravelframework~10 mins

Migration creation in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Migration creation
Run artisan command
Create migration file
Open migration file
Define schema in up()
Define rollback in down()
Run migration
Database updated
The flow shows how running a command creates a migration file, then you define the table schema, and finally run the migration to update the database.
Execution Sample
Laravel
php artisan make:migration create_users_table

// In migration file:
public function up() {
  Schema::create('users', function($table) {
    $table->id();
  });
}

public function down() {
  Schema::dropIfExists('users');
}
This code creates a migration file for a users table, defines the table with an id column, and defines how to drop it on rollback.
Execution Table
StepActionFile Created/ModifiedSchema Method CalledDatabase State
1Run artisan make:migration create_users_tabledatabase/migrations/xxxx_xx_xx_xxxxxx_create_users_table.phpNoneNo users table
2Open migration filedatabase/migrations/xxxx_xx_xx_xxxxxx_create_users_table.phpNoneNo users table
3Define up() method with Schema::create('users')database/migrations/xxxx_xx_xx_xxxxxx_create_users_table.phpcreate('users') with id columnNo users table yet
4Define down() method with Schema::dropIfExists('users')database/migrations/xxxx_xx_xx_xxxxxx_create_users_table.phpdropIfExists('users')No users table yet
5Run php artisan migrateNo file changecreate('users') executedUsers table created in database
6Migration completeNo file changeMigration appliedUsers table exists
💡 Migration applied successfully, users table created in database
Variable Tracker
VariableStartAfter Step 3After Step 5Final
Migration FileNot createdCreated with up() and down()No changeExists with schema methods
Database users tableDoes not existDoes not existCreatedExists
Key Moments - 2 Insights
Why doesn't the users table appear in the database right after creating the migration file?
Because the migration file only defines the schema but does not run it. The table is created only after running 'php artisan migrate' as shown in steps 3 and 5 in the execution_table.
What is the purpose of the down() method in the migration file?
The down() method defines how to undo the migration, usually by dropping the table. This allows rollback if needed, as shown in step 4 where dropIfExists('users') is defined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the users table actually created in the database?
AStep 3
BStep 5
CStep 2
DStep 1
💡 Hint
Check the 'Database State' column in the execution_table rows.
According to the variable tracker, what is the state of the migration file after step 3?
ANot created
BDeleted
CCreated with up() and down() methods
DEmpty file
💡 Hint
Look at the 'Migration File' row in variable_tracker after step 3.
If you forgot to define the down() method, what would happen when you try to rollback the migration?
ARollback would fail or not drop the table properly
BRollback would succeed and drop the table
CMigration would not run at all
DDatabase would be deleted
💡 Hint
Recall the purpose of down() method explained in key_moments and step 4 in execution_table.
Concept Snapshot
Migration creation in Laravel:
- Run 'php artisan make:migration name'
- Edit migration file: define up() to create table
- Define down() to drop table
- Run 'php artisan migrate' to apply
- Use 'php artisan migrate:rollback' to undo
Migrations manage database schema changes safely.
Full Transcript
Migration creation in Laravel starts by running the artisan command to create a migration file. This file contains two methods: up() to define the schema changes like creating a table, and down() to define how to undo those changes. The migration file itself does not change the database until you run 'php artisan migrate', which applies the schema changes and creates the table. The down() method allows rolling back the migration safely. This process helps manage database structure changes step-by-step.