Challenge - 5 Problems
Laravel Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct Laravel migration syntax for creating a table
Which option correctly creates a migration to make a
users table with an id and email column?Attempts:
2 left
💡 Hint
Remember the method to create tables is
Schema::create and $table->id() adds an auto-incrementing ID.✗ Incorrect
Option C uses the correct
Schema::create method and calls $table->id() as a method, which creates an auto-incrementing primary key. Option C uses a non-existent make method. Option C uses integer('id') which does not set the primary key or auto-increment. Option C misses parentheses on $table->id, causing a syntax error.❓ component_behavior
intermediate1:30remaining
Effect of
nullable() in Laravel migration columnsWhat is the effect of adding
->nullable() to a column in a Laravel migration?Attempts:
2 left
💡 Hint
Think about what NULL means in databases.
✗ Incorrect
Adding
->nullable() means the column can store NULL values, which means it can be empty. It does not set default values, primary keys, or indexes.❓ state_output
advanced2:30remaining
Result of running a Laravel migration with a foreign key constraint
Given this migration snippet, what happens when you run it?
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->string('title');
$table->timestamps();
});Attempts:
2 left
💡 Hint
The
constrained() method automatically links to the users table by convention.✗ Incorrect
Option A is correct because
foreignId('user_id')->constrained() creates a foreign key to the users table's id column by default. The onDelete('cascade') means if a user is deleted, their posts are deleted too. Option A misses the constraint. Option A is wrong because the table name is optional if it follows Laravel conventions. Option A is wrong because the column is not nullable unless specified.🔧 Debug
advanced2:00remaining
Identify the error in this Laravel migration code
What error will this migration code cause when running
php artisan migrate?Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->string('content');
$table->foreignId('post_id')->constrained('posts');
$table->timestamps();
$table->foreign('post_id')->references('id')->on('posts');
});Attempts:
2 left
💡 Hint
Check how foreign keys are defined and avoid repeating them.
✗ Incorrect
The code defines a foreign key twice on the same column: once with
foreignId('post_id')->constrained('posts') and again with foreign('post_id')->references('id')->on('posts'). This causes a duplicate foreign key error. Option A is wrong because $table->id() defines the primary key. Option A is wrong because constrained() can take a table name argument. Option A is incorrect because the migration will fail.🧠 Conceptual
expert2:30remaining
Understanding Laravel migration rollback behavior
After running multiple migrations, you execute
php artisan migrate:rollback. What exactly happens?Attempts:
2 left
💡 Hint
Think about how Laravel groups migrations into batches.
✗ Incorrect
Laravel groups migrations into batches when running them. The rollback command reverses the last batch of migrations, not all migrations or just one file. Option D is incorrect because it would require multiple rollbacks. Option D is wrong because rollback works by batch, not single file. Option D is wrong because rollback defaults to one batch if no steps specified.