Schema builder helps you create and change database tables easily using code. It saves time and avoids manual work.
Schema builder (columns, types) in 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.
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.Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->integer('age'); $table->timestamps(); });
content and a boolean published column with default false to the existing posts table.Schema::table('posts', function (Blueprint $table) { $table->text('content')->nullable(); $table->boolean('published')->default(false); });
price with 8 digits total and 2 digits after the decimal point.$table->decimal('price', 8, 2);
This migration creates a products table with columns for ID, name, optional description, price with decimals, a boolean for stock status, and timestamps.
<?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'); } }
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.
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
Schema::create method do in Laravel migrations?Solution
Step 1: Understand the purpose of
This method is used to define a new table structure in the database.Schema::createStep 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 AQuick Check:
Schema::create = create table [OK]
- Confusing create with drop or update
- Thinking it modifies data instead of structure
- Assuming it runs raw SQL
title in a Laravel migration?Solution
Step 1: Identify the correct method for string columns
Laravel usesstring()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 BQuick Check:
String column = $table->string('name') [OK]
- Using varchar() which does not exist
- Confusing text() with string()
- Using char() without length
$table->integer('age')->unsigned();Solution
Step 1: Understand the
This creates an integer type column in the database.integer()methodStep 2: Analyze the effect of
Theunsigned()unsigned()modifier makes the integer only accept positive values (no negatives).Final Answer:
Unsigned integer column -> Option AQuick Check:
integer()->unsigned() = unsigned int [OK]
- Ignoring unsigned modifier
- Thinking integer() creates string
- Confusing with boolean type
Schema::create('users', function (Blueprint $table) {
$table->string('email')->nullable;
});Solution
Step 1: Check the nullable method usage
In Laravel,nullableis a method and must be called with parentheses().Step 2: Identify the missing parentheses
The code usesnullablewithout(), causing a syntax error.Final Answer:
Missing parentheses after nullable method -> Option DQuick Check:
nullable() requires parentheses [OK]
- Writing nullable without ()
- Using allowNull() instead of nullable()
- Assuming string can't be nullable
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?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 CQuick Check:
increments + nullable() + decimal(8,2) = correct [OK]
- Forgetting parentheses on nullable()
- Using autoIncrement() instead of increments()
- Missing decimal precision arguments
Ready to prove it?
Pass the test and this pattern moves unknown
