The Database Forge Class in CodeIgniter 4 is a powerful utility that allows developers to manage database schema directly from PHP code. It provides programmatic methods for creating, modifying, and dropping tables and fields without manually writing SQL statements. This makes database migrations, table alterations, and schema updates easier and more reliable.
In this tutorial, we’ll explore how to create tables, add and modify columns, and drop tables and columns using the Forge Class — all through clean PHP code, without raw SQL queries.
Let’s get started.
⚪ What is the Database Forge Class?
The Database Forge Class is part of CodeIgniter 4’s database utilities, designed to interact with database schema structures. It can be used to create new tables, add or drop columns, modify fields, and delete tables — all through clean, readable PHP code instead of raw SQL queries.
Load Forge Instance:
$forge = \Config\Database::forge();
⚪ Creating a New Table
We can create a new table by defining an array of fields and then calling the createTable() method.
Example:
$forge = \Config\Database::forge();
$fields = [
'id' => [
'type' => 'INT',
'constraint' => 5,
'unsigned' => true,
'auto_increment' => true
],
'name' => [
'type' => 'VARCHAR',
'constraint' => '100'
]
];
$forge->addField($fields);
$forge->addKey('id', true);
$forge->createTable('products');
⚪ Adding a New Column
We can add a new column to an existing table using the addColumn() method.
Example:
$forge->addColumn('products', [
'description' => [
'type' => 'TEXT',
'null' => true
]
]);
⚪ Modifying an Existing Column
To modify an existing field’s properties, use the modifyColumn() method.
Example:
$forge->modifyColumn('products', [
'name' => [
'name' => 'product_name',
'type' => 'VARCHAR',
'constraint' => '150'
]
]);
⚪ Dropping a Column
Remove a column from a table using dropColumn().
Example:
$forge->dropColumn('products', 'description');
⚪ Dropping a Table
To completely remove a table from our database, use dropTable().
$forge->dropTable('products');
⚪ Complete Example: Creating users Table
Here’s a complete example to create a users table with multiple fields including id, name, email, gender, age, status, and created_date using the Database Forge Class.
namespace App\Controllers;
use CodeIgniter\Controller;
use Config\Database;
class ForgeController extends Controller
{
public function createUsersTable()
{
$forge = Database::forge();
$fields = [
'id' => [
'type' => 'INT',
'constraint' => 5,
'unsigned' => true,
'auto_increment' => true
],
'name' => [
'type' => 'VARCHAR',
'constraint' => '100'
],
'email' => [
'type' => 'VARCHAR',
'constraint' => '150'
],
'gender' => [
'type' => 'ENUM',
'constraint' => ['male', 'female']
],
'age' => [
'type' => 'INT',
'constraint' => 3,
'null' => true
],
'status' => [
'type' => 'TINYINT',
'constraint' => 1,
'default' => 1
],
'created_date' => [
'type' => 'DATETIME',
'default' => 'CURRENT_TIMESTAMP'
]
];
$forge->addField($fields);
$forge->addKey('id', true);
$forge->createTable('users');
echo "Users table created successfully.";
}
}
⚪ Conclusion
The CodeIgniter 4 Database Forge Class simplifies database schema management by replacing complex SQL queries with clean, structured PHP code.
It’s especially useful for database migrations and dynamic schema changes, making our development workflow faster, safer, and more maintainable.