Laravel's Schema Builder helps you create and modify database tables using PHP code instead of writing raw SQL. It makes managing database structure easier and more readable.
Click to reveal answer
beginner
How do you add a string column named 'title' to a table using Laravel's Schema Builder?
Use the method $table->string('title'); inside the Schema::create or Schema::table callback to add a string column called 'title'.
Click to reveal answer
beginner
Name three common column types you can define with Laravel's Schema Builder.
Common column types include:
string for short text
integer for whole numbers
boolean for true/false values
Click to reveal answer
intermediate
What method would you use to add a nullable column in Laravel's Schema Builder?
You chain the nullable() method to the column definition, like $table->string('description')->nullable(); to allow the column to hold NULL values.
Click to reveal answer
beginner
How do you create an auto-incrementing primary key column in Laravel's Schema Builder?
Use $table->id(); which creates an unsigned big integer column named 'id' that auto-increments and is the primary key.
Click to reveal answer
Which method adds a date and time column in Laravel's Schema Builder?
A$table->boolean('created_at');
B$table->string('created_at');
C$table->integer('created_at');
D$table->dateTime('created_at');
✗ Incorrect
The dateTime method adds a column that stores date and time values.
How do you make a column optional (can be NULL) in Laravel's Schema Builder?
AAdd <code>->optional()</code> after the column type
BAdd <code>->nullable()</code> after the column type
CAdd <code>->default(NULL)</code> after the column type
DAdd <code>->null()</code> after the column type
✗ Incorrect
The nullable() method allows the column to accept NULL values.
What does $table->timestamps(); do in a migration?
AAdds 'created_at' and 'updated_at' timestamp columns
BAdds a single timestamp column named 'timestamps'
CAdds a boolean column named 'timestamps'
DAdds an integer column named 'timestamps'
✗ Incorrect
The timestamps() method adds two columns: created_at and updated_at.
Which column type is best for storing true/false values?
Aboolean
Btext
Cinteger
Dstring
✗ Incorrect
The boolean type stores true or false values efficiently.
How do you drop a column named 'age' using Schema Builder?
A$table->deleteColumn('age');
B$table->remove('age');
C$table->dropColumn('age');
D$table->drop('age');
✗ Incorrect
The correct method to remove a column is dropColumn().
Explain how to add different column types using Laravel's Schema Builder and give examples.
Think about methods like string(), integer(), boolean(), nullable(), and id().
You got /5 concepts.
Describe how Laravel's Schema Builder helps manage database tables compared to writing raw SQL.
Focus on benefits of using Schema Builder for beginners and teams.
You got /5 concepts.
Practice
(1/5)
1. What does the Schema::create method do in Laravel migrations?
easy
A. Creates a new database table with specified columns
B. Deletes an existing database table
C. Updates data inside a table
D. Runs raw SQL queries directly
Solution
Step 1: Understand the purpose of Schema::create
This method is used to define a new table structure in the database.
Step 2: Recognize the role of columns inside the callback
The callback function receives a Blueprint object to define columns for the new table.
Final Answer:
Creates a new database table with specified columns -> Option A
Quick Check:
Schema::create = create table [OK]
Hint: Schema::create always defines a new table [OK]
Common Mistakes:
Confusing create with drop or update
Thinking it modifies data instead of structure
Assuming it runs raw SQL
2. Which of the following is the correct syntax to add a string column named title in a Laravel migration?
easy
A. $table->varchar('title');
B. $table->string('title');
C. $table->text('title');
D. $table->char('title');
Solution
Step 1: Identify the correct method for string columns
Laravel uses string() to create VARCHAR columns.
Step 2: Check the syntax for adding a column
The syntax is $table->string('column_name'); which matches $table->string('title');.
Final Answer:
$table->string('title'); -> Option B
Quick Check:
String column = $table->string('name') [OK]
Hint: Use $table->string() for VARCHAR columns [OK]
Common Mistakes:
Using varchar() which does not exist
Confusing text() with string()
Using char() without length
3. What will be the resulting column type in the database for this migration code?
$table->integer('age')->unsigned();
medium
A. Unsigned integer column
B. Signed integer column
C. String column named 'age'
D. Boolean column
Solution
Step 1: Understand the integer() method
This creates an integer type column in the database.
Step 2: Analyze the effect of unsigned()
The unsigned() modifier makes the integer only accept positive values (no negatives).
Final Answer:
Unsigned integer column -> Option A
Quick Check:
integer()->unsigned() = unsigned int [OK]
Hint: unsigned() means no negative numbers allowed [OK]
Common Mistakes:
Ignoring unsigned modifier
Thinking integer() creates string
Confusing with boolean type
4. Identify the error in this migration code snippet:
Schema::create('users', function (Blueprint $table) { $table->string('email')->nullable; });
medium
A. String columns cannot be nullable
B. Wrong method name, should be allowNull()
C. Blueprint object is not imported
D. Missing parentheses after nullable method
Solution
Step 1: Check the nullable method usage
In Laravel, nullable is a method and must be called with parentheses ().
Step 2: Identify the missing parentheses
The code uses nullable without (), causing a syntax error.
Final Answer:
Missing parentheses after nullable method -> Option D
Quick Check:
nullable() requires parentheses [OK]
Hint: Always add () after nullable method [OK]
Common Mistakes:
Writing nullable without ()
Using allowNull() instead of nullable()
Assuming string can't be nullable
5. You want to create a table with a primary key id that auto-increments, a nullable description text column, and a price decimal column with 8 digits total and 2 decimal places. Which migration code is correct?
hard
A. Schema::create('products', function (Blueprint $table) { $table->bigIncrements('id'); $table->text('description'); $table->decimal('price', 8, 2); });
B. Schema::create('products', function (Blueprint $table) { $table->integer('id')->autoIncrement(); $table->string('description')->nullable; $table->decimal('price', 8); });
C. Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->text('description')->nullable(); $table->decimal('price', 8, 2); });
D. Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->text('description')->nullable; $table->decimal('price', 8, 2); });
Solution
Step 1: Check primary key and auto-increment
$table->increments('id') creates an auto-incrementing primary key integer column.
Step 2: Verify nullable text column syntax
$table->text('description')->nullable(); correctly makes the text column nullable with parentheses.
Step 3: Confirm decimal column precision
$table->decimal('price', 8, 2); defines a decimal with 8 digits total and 2 decimal places.
Final Answer:
Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->text('description')->nullable(); $table->decimal('price', 8, 2); }); -> Option C