Bird
Raised Fist0
Laravelframework~5 mins

Schema builder (columns, types) in Laravel

Choose your learning style10 modes available
Introduction

Schema builder helps you create and change database tables easily using code. It saves time and avoids manual work.

When you want to create a new database table for your app.
When you need to add or change columns in an existing table.
When you want to define the type of data each column holds.
When you want to keep your database structure consistent across different environments.
When you want to share database changes with your team using code.
Syntax
Laravel
Schema::create('table_name', function (Blueprint $table) {
    $table->type('column_name');
});

Use Schema::create to make a new table.

Inside the function, use $table->type('column_name') to add columns with specific data types.

Examples
This creates a users table with an auto-increment ID, a name column as text, an age column as number, and timestamp columns for created and updated times.
Laravel
Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->integer('age');
    $table->timestamps();
});
This adds a nullable text column content and a boolean published column with default false to the existing posts table.
Laravel
Schema::table('posts', function (Blueprint $table) {
    $table->text('content')->nullable();
    $table->boolean('published')->default(false);
});
Adds a decimal column named price with 8 digits total and 2 digits after the decimal point.
Laravel
$table->decimal('price', 8, 2);
Sample Program

This migration creates a products table with columns for ID, name, optional description, price with decimals, a boolean for stock status, and timestamps.

Laravel
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->decimal('price', 8, 2);
            $table->boolean('in_stock')->default(true);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('products');
    }
}
OutputSuccess
Important Notes

Always run migrations using php artisan migrate to apply schema changes.

Use nullable() to allow empty values in a column.

Use default() to set a default value for a column.

Summary

Schema builder lets you create and modify database tables using simple code.

You define columns and their data types inside a function with $table.

This approach keeps your database structure organized and easy to share.

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