Beginner LevelLaravel
How do Laravel Migrations and Schema Builder handle database version control?
❓ Interview Prompt: Explain database migrations, up() and down() methods, Schema builder columns, indexes, and Artisan migration commands.
Migrations are like version control for your database, allowing teams to define and modify database schema using PHP code.
Structure:
•
'up()': Executes schema changes (creating tables, adding columns).
•
'down()': Reverts schema changes during rollbacks ('php artisan migrate:rollback').
Schema Builder:
Provides fluent column definitions like '$table->id()', '$table->string("email")->unique()', '$table->foreignId("user_id")->constrained()'.
Code Implementation Example
php
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration {
public function up(): void {
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->timestamps();
});
}
};💡Laravel Schema Builder migration creating posts table with foreign keys.
🎯 Interview Key Takeaway
Migrations provide team-wide database version control using expressive PHP Schema Builder methods.
#Migrations#Schema Builder#Database#Beginner
Back to all Laravel Questions→