Seeder or seedbeds in CodeIgniter 4 to generate test data

- Andrés Cruz

ES En español

Seeder or seedbeds in CodeIgniter 4 to generate test data

Seeders are the mechanism we have to generate test data in a simple way; they are strongly linked to the models in CodeIgniter 4, since the problem with the migrations is that when we rollback, we lose all the test data... but with the seeders we can get this data back in a very short time; instead of creating it manually.

We can generate the seeders or seedbeds via the command line; following the recommended schema, we should create one per entity or at least one; but we must avoid handling multiple entities from a single file, but you could, for example, if you see it necessary, generate two seedbeds to generate different users, with different roles.

Generate the seeder using spark

The command is:

php spark db:seed UserSeeder

Where UserSeeder is the name you want to give to the file and class

We have a function called run, which is what we use to generate the test data, either individually or in a cycle based on some condition; in our case, we generate about 6 test users

public function run()
    {
        $userModel = new UserModel();
        for ($i = 0; $i < 5; $i++) {
            $userModel->insert([
                'username' => 'user' . $i,
                'email' => "user$i@gmail.com",
                'password' => '12345'
            ]);
        }
    }

Then, to run the seeder:

php spark make:seeder UserSeeder

Where <UserSeeder> is the name you want to give your seeder; once defined, we execute it as follows:

$ php spark db:seed <SeederName>

And voila, when we review the database, we will see our generated users.

Nested seeders

Seeders are a fairly automated solution, but running one seeder at a time via the command line may not be the best option for you if you have a lot of models defined in your application, in which case it is possible to nest seeders; for that, we can create a master seeder:

$ php spark make:seeder TodosSeeder

In which, using the call() function, we specify the name of the seeders we want to nest or group in this seeder; so, we run the seeder called TodosSeeder:

app\Database\Seeds\TodoSeeder.php

namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
class TodosSeeder extends Seeder
{
    public function run()
    {
        $this->call('PeliculaSeeder');
        $this->call('CategoriaSeeder');
    }
}

And as a result, it will appear that all our seeders were executed.

Seeded: App\Database\Seeds\PeliculaSeeder
Seeded: App\Database\Seeds\CategoriaSeeder
Seeded: App\Database\Seeds\TodosSeeder

An interesting case to finish with is that if, when executing the db:seed command, you don't pass any arguments:

$ php spark db:seed

Spark will ask you for the name of the seeders class, which you want to run:

CodeIgniter v4.1.7 Command Line Tool - Server Time: 2026-02-09 14:31:12 UTC-06:00
Seeder name : PeliculaSeeder                                                                                                          
Seeded: App\Database\Seeds\PeliculaSeeder
PS C:\laragon\www\test\peliculas>

The next step is to learn about the command-line tool in CodeIgniter 4, Spark.
Seeders are the mechanism we have to generate test data in a simple way; Let's see how to create them, generate the data and execute them using spark and CodeIgniter 4.

I agree to receive announcements of interest about this Blog.

Andrés Cruz

ES En español