Advertisement
Advanced PHP, Eloquent ORM & Doctrine ORM

How do database migrations work alongside an ORM like Eloquent or Doctrine to manage schema changes in a version controlled, repeatable way?

Short answer

Database migrations are version controlled files describing incremental changes to your database schema, such as adding a new column or creating a new table, and both Eloquent through Laravel's migration system and Doctrine through its own migrations bundle let you write these…

Save PHP mock interview
Advertisement
AModel answer
Database migrations are version controlled files describing incremental changes to your database schema, such as adding a new column or creating a new table, and both Eloquent through Laravel's migration system and Doctrine through its own migrations bundle let you write these schema changes as PHP code that can be run consistently across every environment, tracked in version control alongside your application code, and rolled back if a specific change needs to be undone, ensuring your database schema stays synchronized with your application's code across every developer's machine and every deployment environment.
Schema::create('products', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->decimal('price', 8, 2);
    $table->timestamps();
});
Real-world example
A team adds a new required column to their products table by writing a migration file, ensuring every developer's local database and their production database all receive the exact same schema change consistently and in the correct order.
Likely follow-ups
  • How do you safely roll back a migration that has already been run in production?
  • What happens if two developers create conflicting migrations at the same time?
Related topics