CodeIgniter 4 provides a powerful migration system to create and manage database tables. While the recommended approach is using php spark migrate, some developers may prefer creating tables manually, especially if they do not have access to the command line or prefer working directly with PHP scripts.
In this guide, we will cover how to create tables in CodeIgniter 4 without using php spark by leveraging the built-in migration feature programmatically.
Step 1: Enable Migrations in CodeIgniter 4
Before creating tables, ensure that migrations are enabled in your CodeIgniter 4 project.
Open
app/Config/Migrations.phpSet
$enabledtotrue:public $enabled = true;
Step 2: Create a Migration File Manually
Instead of using php spark make:migration, create a migration file manually inside the app/Database/Migrations directory.
Navigate to
app/Database/Migrations/Create a PHP file with a meaningful name, e.g.,
2025-02-02-CreateUsersTable.php
Step 3: Define the Migration Class
Open the newly created file and define the table structure:
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => '100',
],
'email' => [
'type' => 'VARCHAR',
'constraint' => '100',
'unique' => true,
],
'password' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
],
'updated_at' => [
'type' => 'DATETIME',
'null' => true,
],
'deleted_at' => [
'type' => 'DATETIME',
'null' => true,
],
]);
$this->forge->addKey('id', true);
$this->forge->createTable('users');
}
public function down()
{
$this->forge->dropTable('users');
}
}Step 4: Run the Migration Without PHP Spark
Since we are not using php spark migrate, we need to execute the migration manually.
Open
app/Controllers/Migrate.phpCreate a new controller (if not already created):
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\Database\Migrations\Runner;
class Migrate extends Controller
{
public function index()
{
$migrations = service('migrations');
try {
$migrations->latest();
echo "Migration completed successfully.";
} catch (Exception $e) {
echo "Migration failed: " . $e->getMessage();
}
}
public function rollback()
{
$migrations = service('migrations');
try {
$migrations->regress(0);
echo "Rollback completed successfully.";
} catch (Exception $e) {
echo "Rollback failed: " . $e->getMessage();
}
}
}Step 5: Execute the Migration from the Browser
Start your CodeIgniter server:
php -S localhost:8080 -t publicOpen your browser and visit:
http://localhost:8080/migrateIf everything is set up correctly, you should see:
Migration completed successfully.Check your database to confirm that the
userstable has been created.
How to Drop the Table
If you want to remove the table, you can trigger the down method in the migration file.
Open your browser and visit:
http://localhost:8080/migrate/rollbackThis will execute
down(), which removes theuserstable.Check your database to confirm that the table has been dropped.
Conclusion
By following these steps, you can manually create and drop tables in CodeIgniter 4 without using the php spark command. This approach is useful when working in restricted environments or when automating migrations via controllers.
Happy coding!
0 Comments:
Post a Comment