0
0
Laravelframework~10 mins

Schema builder (columns, types) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Schema builder (columns, types)
Start Migration
Create Schema Builder Instance
Define Table Name
Add Columns with Types
Set Column Options (nullable, default, etc.)
Run Migration to Create Table
Table Created in Database
This flow shows how Laravel's schema builder creates a table by defining columns and their types step-by-step.
Execution Sample
Laravel
Schema::create('users', function (Blueprint $table) {
  $table->id();
  $table->string('name');
  $table->integer('age')->nullable();
  $table->timestamps();
});
This code creates a 'users' table with an id, a name column, an optional age column, and timestamp columns.
Execution Table
StepActionColumn AddedTypeOptions
1Start Schema::createNoneNoneNone
2$table->id()idbigint (auto-increment)Primary key
3$table->string('name')namestring (varchar)Required
4$table->integer('age')->nullable()ageintegerNullable
5$table->timestamps()created_at, updated_attimestampNullable, default NULL
6End Schema::createTable 'users' createdN/AN/A
💡 All columns defined and table created in database
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
columns[][id][id, name][id, name, age][id, name, age, created_at, updated_at][id, name, age, created_at, updated_at]
Key Moments - 2 Insights
Why does $table->integer('age')->nullable() allow null values?
Because the nullable() method sets the column to accept NULL, as shown in step 4 of the execution_table where 'age' column options include Nullable.
What does $table->timestamps() add to the table?
It adds two timestamp columns: created_at and updated_at, which are nullable by default, as shown in step 5 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what type is assigned to the 'id' column at step 2?
Ainteger
Bstring
Cbigint (auto-increment)
Dtimestamp
💡 Hint
Check the 'Type' column in row for step 2 in execution_table
At which step are the timestamp columns created?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for $table->timestamps() action in execution_table
If we remove ->nullable() from the 'age' column, what changes in the options column at step 4?
AOptions will be empty (not nullable)
BOptions will include Nullable
CType changes to string
DColumn is removed
💡 Hint
Compare options for 'age' column in step 4 with and without nullable() in execution_table
Concept Snapshot
Laravel Schema Builder:
- Use Schema::create('table', fn(Blueprint $table) => {...})
- Add columns: $table->string('name'), $table->integer('age')->nullable()
- Common types: id(), string(), integer(), timestamps()
- Use nullable() to allow NULL values
- Run migration to create table in DB
Full Transcript
This visual execution shows how Laravel's schema builder creates a database table step-by-step. First, the migration starts and Schema::create is called with the table name 'users'. Then columns are added one by one: an auto-incrementing id, a required string 'name', an integer 'age' that can be null, and timestamp columns for created_at and updated_at. Each step updates the columns list. Nullable columns allow empty values. The timestamps method adds two timestamp columns. Finally, the table is created in the database with all defined columns.