14 February 2024

To add a migration file in CodeIgniter 4, you can use the command-line interface provided by CodeIgniter's CLI tool called spark.

Create migration template

Navigate to your CodeIgniter 4 project directory. Run the following command to generate a migration file. This will create a migration file named yyyy-mm-dd-Hi_CreateUsersTable.php in the app\Database\Migrations directory, where yyyy-mm-dd-Hi is the timestamp of the migration creation.
Source code viewer
  1. php spark migrate:create YourMigrationName
Programming Language: Bash

Implement your migration

Open the newly created migration file in your text editor and define the up and down methods. The up method is used to define the actions that should be performed when migrating up, and the down method should define the actions to rollback the migration.

Run migration

Once you've defined your migration logic, you can run the migration using the following command.
Source code viewer
  1. php spark migrate
Programming Language: Bash