Bird
Raised Fist0
Laravelframework~30 mins

Schema builder (columns, types) in Laravel - Mini Project: Build & Apply

Choose your learning style10 modes available
Laravel Schema Builder: Create a Users Table
📖 Scenario: You are building a simple user management system. You need to create a database table to store user information like name, email, and age.
🎯 Goal: Create a Laravel migration using the Schema builder to define a users table with specific columns and types.
📋 What You'll Learn
Create a migration file with a users table
Add columns with correct data types using Laravel Schema builder
Use the up method to define the table structure
Include a primary key column id with auto-increment
💡 Why This Matters
🌍 Real World
Creating database tables with migrations is a common task in Laravel projects to manage data structure safely and consistently.
💼 Career
Understanding Laravel's Schema builder is essential for backend developers working with Laravel to build and maintain databases.
Progress0 / 4 steps
1
Create the users table with an id column
Write the up method inside the migration class. Use Schema::create to create a table named users. Inside the callback, add an id column using $table->id().
Laravel
Hint

Use Schema::create('users', function (Blueprint $table) { ... }) and inside add $table->id();

2
Add name and email columns
Inside the Schema::create callback, add a name column of type string and an email column of type string using $table->string('name') and $table->string('email').
Laravel
Hint

Use $table->string('name'); and $table->string('email'); inside the callback.

3
Add an age column with integer type
Add an age column of type integer using $table->integer('age') inside the Schema::create callback.
Laravel
Hint

Use $table->integer('age'); inside the callback.

4
Add timestamps columns
Add Laravel's automatic timestamp columns created_at and updated_at by calling $table->timestamps() inside the Schema::create callback.
Laravel
Hint

Use $table->timestamps(); to add created_at and updated_at columns.

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