Bird
Raised Fist0
Laravelframework~10 mins

Schema builder (columns, types) in Laravel - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a string column named 'title' to the table.

Laravel
Schema::create('posts', function (Blueprint $table) {
    $table->[1]('title');
});
Drag options to blanks, or click blank then click option'
Ainteger
Btext
Cboolean
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' instead of 'string' for short text columns.
2fill in blank
medium

Complete the code to add an integer column named 'age' to the table.

Laravel
Schema::table('users', function (Blueprint $table) {
    $table->[1]('age');
});
Drag options to blanks, or click blank then click option'
Ainteger
Bdate
Cstring
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' for numeric columns.
3fill in blank
hard

Fix the error in the code to add a boolean column named 'is_active'.

Laravel
Schema::table('accounts', function (Blueprint $table) {
    $table->[1]('is_active');
});
Drag options to blanks, or click blank then click option'
Ainteger
Bstring
Cboolean
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'integer' or 'string' instead of 'boolean' for flags.
4fill in blank
hard

Fill both blanks to add a decimal column named 'price' with 8 digits total and 2 decimal places.

Laravel
Schema::table('products', function (Blueprint $table) {
    $table->[1]('price', [2], 2);
});
Drag options to blanks, or click blank then click option'
Adecimal
Binteger
C8
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'integer' instead of 'decimal' for prices.
5fill in blank
hard

Fill all three blanks to add a string column 'email' that is unique and nullable.

Laravel
Schema::table('users', function (Blueprint $table) {
    $table->[1]('email')->[2]()->[3]();
});
Drag options to blanks, or click blank then click option'
Astring
Bunique
Cnullable
Dinteger
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add 'unique' or 'nullable' methods.

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

  1. Step 1: Understand the purpose of Schema::create

    This method is used to define a new table structure in the database.
  2. Step 2: Recognize the role of columns inside the callback

    The callback function receives a Blueprint object to define columns for the new table.
  3. Final Answer:

    Creates a new database table with specified columns -> Option A
  4. 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

  1. Step 1: Identify the correct method for string columns

    Laravel uses string() to create VARCHAR columns.
  2. Step 2: Check the syntax for adding a column

    The syntax is $table->string('column_name'); which matches $table->string('title');.
  3. Final Answer:

    $table->string('title'); -> Option B
  4. 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

  1. Step 1: Understand the integer() method

    This creates an integer type column in the database.
  2. Step 2: Analyze the effect of unsigned()

    The unsigned() modifier makes the integer only accept positive values (no negatives).
  3. Final Answer:

    Unsigned integer column -> Option A
  4. 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

  1. Step 1: Check the nullable method usage

    In Laravel, nullable is a method and must be called with parentheses ().
  2. Step 2: Identify the missing parentheses

    The code uses nullable without (), causing a syntax error.
  3. Final Answer:

    Missing parentheses after nullable method -> Option D
  4. 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

  1. Step 1: Check primary key and auto-increment

    $table->increments('id') creates an auto-incrementing primary key integer column.
  2. Step 2: Verify nullable text column syntax

    $table->text('description')->nullable(); correctly makes the text column nullable with parentheses.
  3. Step 3: Confirm decimal column precision

    $table->decimal('price', 8, 2); defines a decimal with 8 digits total and 2 decimal places.
  4. Final Answer:

    Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->text('description')->nullable(); $table->decimal('price', 8, 2); }); -> Option C
  5. Quick Check:

    increments + nullable() + decimal(8,2) = correct [OK]
Hint: Use increments() for PK, nullable() with (), decimal needs precision [OK]
Common Mistakes:
  • Forgetting parentheses on nullable()
  • Using autoIncrement() instead of increments()
  • Missing decimal precision arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard