This migration creates a new table called books with columns for id, title, author, and timestamps for created and updated times.
Laravel
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('author');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('books');
}
};
OutputSuccess
Important Notes
Run php artisan migrate to apply migrations to your database.
Use php artisan migrate:rollback to undo the last migration.
Always write the down method to safely reverse changes.
Summary
Migrations help manage database tables easily and safely.
Use php artisan make:migration with --create or --table to generate migration files.
Run migrations with php artisan migrate to update your database.
Practice
(1/5)
1. What is the primary purpose of a migration in Laravel?
easy
A. To create and modify database tables in a structured way
B. To handle user authentication
C. To manage routes in the application
D. To compile CSS and JavaScript assets
Solution
Step 1: Understand migration role
Migrations are designed to manage database schema changes safely and consistently.
Step 2: Compare with other options
Options A, C, and D relate to authentication, routing, and asset compilation, which are not migration tasks.
Final Answer:
To create and modify database tables in a structured way -> Option A
Quick Check:
Migration = Manage database tables [OK]
Hint: Migrations always deal with database tables [OK]
Common Mistakes:
Confusing migrations with routing or authentication
Thinking migrations compile assets
Assuming migrations handle user sessions
2. Which command correctly creates a new migration file in Laravel?
easy
A. php artisan migration:create users
B. php artisan make:migration create_users_table
C. php artisan new:migration users_table
D. php artisan create:migration users
Solution
Step 1: Recall Laravel migration command
The correct command to create a migration is php artisan make:migration followed by the migration name.
Step 2: Check each option
Only php artisan make:migration create_users_table uses the correct syntax and command name.
Final Answer:
php artisan make:migration create_users_table -> Option B
Quick Check:
Use 'make:migration' to create migrations [OK]
Hint: Use 'make:migration' command to create migrations [OK]
Common Mistakes:
Using wrong artisan command names
Omitting 'make:' prefix
Confusing migration creation with other artisan commands
3. Given this migration snippet, what will happen when running php artisan migrate?
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
medium
A. Updates the 'posts' table by adding new columns
B. Deletes the 'posts' table if it exists
C. Creates a 'posts' table with id, title, content, and timestamps columns
D. Throws an error because 'timestamps' is not a valid method
Solution
Step 1: Analyze the up() method
The code uses Schema::create to make a new table named 'posts' with columns: id, title, content, and timestamps.
Step 2: Understand Laravel Schema methods
$table->timestamps() adds created_at and updated_at columns automatically. This is valid and common.
Final Answer:
Creates a 'posts' table with id, title, content, and timestamps columns -> Option C
Quick Check:
Schema::create + columns = new table [OK]
Hint: Schema::create builds new tables with specified columns [OK]
Common Mistakes:
Thinking timestamps() is invalid
Confusing create with update
Assuming it deletes tables
4. Identify the error in this migration code:
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->timestamps;
});
}
medium
A. String column 'text' must have a length specified
B. Using increments instead of id method
C. Table name should be singular, not plural
D. Missing parentheses after timestamps method call
Solution
Step 1: Check method calls in migration
$table->timestamps is missing parentheses. It should be $table->timestamps() to call the method.
Step 2: Validate other parts
Using increments('id') is valid, plural table names are allowed, and string columns default length is 255 if not specified.
Final Answer:
Missing parentheses after timestamps method call -> Option D
Quick Check:
Method calls need parentheses () [OK]
Hint: Always add () when calling migration methods [OK]
Common Mistakes:
Forgetting parentheses on method calls
Thinking increments is invalid
Believing table names must be singular
5. You want to create a migration that adds a nullable 'bio' text column to the existing 'users' table. Which up() method code is correct?
hard
A. Schema::table('users', function (Blueprint $table) {
$table->text('bio')->nullable();
});
B. Schema::create('users', function (Blueprint $table) {
$table->text('bio')->nullable();
});
C. Schema::modify('users', function (Blueprint $table) {
$table->text('bio')->nullable();
});
D. Schema::addColumn('users', 'bio', 'text')->nullable();
Solution
Step 1: Understand how to modify existing tables
To add a column to an existing table, use Schema::table with a closure defining the new column.
Step 2: Evaluate each option
Schema::table('users', function (Blueprint $table) {
$table->text('bio')->nullable();
}); correctly uses Schema::table and adds a nullable text column. Schema::create('users', function (Blueprint $table) {
$table->text('bio')->nullable();
}); tries to create a new table, which is wrong. Options A and D use invalid Schema methods.
Final Answer:
Schema::table('users', function (Blueprint $table) {
$table->text('bio')->nullable();
}); -> Option A
Quick Check:
Use Schema::table to add columns [OK]
Hint: Use Schema::table to change existing tables [OK]
Common Mistakes:
Using Schema::create instead of Schema::table
Using non-existent Schema methods
Forgetting nullable() for optional columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.