Creating Tables in CodeIgniter 4 Without PHP Spark

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.

  1. Open app/Config/Migrations.php

  2. Set $enabled to true:

    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.

  1. Navigate to app/Database/Migrations/

  2. 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.

  1. Open app/Controllers/Migrate.php

  2. Create 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

  1. Start your CodeIgniter server: php -S localhost:8080 -t public

  2. Open your browser and visit: http://localhost:8080/migrate

  3. If everything is set up correctly, you should see: Migration completed successfully.

  4. Check your database to confirm that the users table 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.

  1. Open your browser and visit: http://localhost:8080/migrate/rollback

  2. This will execute down(), which removes the users table.

  3. 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!

Share:

0 Comments:

Post a Comment