CodeIgniter 4 Database Seeders Complete Tutorial 🚀

Reading Time: 5 minutes
12 Views

When developing web applications, you often need to populate your database with sample or default data for testing and development purposes. In CodeIgniter 4, this is handled efficiently using Database Seeders.

In this tutorial, we’ll cover:

  • What are Seeders in CodeIgniter 4
  • How to create and run seeders
  • How to use multiple seeders
  • How to call seeders within migrations or controllers
  • What are Best practices for seeding data?

Let’s get started.

📌 What is a Seeder in CodeIgniter 4?

A Seeder is a simple class responsible for populating your database tables with initial or testing data. Instead of manually inserting records, you can automate this using Seeders through the Spark CLI or programmatically within your code.

📌 Why Use Seeders?

  • Quickly populate tables with dummy or initial data
  • Test application features with consistent sample data
  • Reset and reseed databases during development
  • Automate data creation for staging or demo environments

📌 How to Create a Seeder

We can easily create a new seeder using the Spark CLI:

php spark make:seeder PostSeeder

This will generate a new file at,

app/Database/Seeds/PostSeeder.php

📌 Seeder Structure Example

A basic seeder looks like this:

<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class PostSeeder extends Seeder
{
    public function run()
    {
        $data = [
            'title'   => 'First Blog Post',
            'content' => 'This is a sample blog post content.',
        ];

        // Insert data into the 'posts' table
        $this->db->table('posts')->insert($data);
    }
}

📌 How to Run a Seeder

To execute a seeder:

php spark db:seed PostSeeder

Output:

Seeded: PostSeeder

📌 Inserting Multiple Records

To insert multiple records at once:

public function run()
{
    $data = [
        [
            'title'   => 'First Post',
            'content' => 'Content for first post.',
        ],
        [
            'title'   => 'Second Post',
            'content' => 'Content for second post.',
        ],
    ];

    $this->db->table('posts')->insertBatch($data);
}

📌 Calling a Seeder Programmatically

We can also run a seeder from another seeder, a migration, or a controller:

$this->call('PostSeeder');

Example within another seeder:

public function run()
{
    $this->call('PostSeeder');
    $this->call('UserSeeder');
}

📌 Using Faker to Generate Random Data

CodeIgniter 4 integrates the Faker library for generating dummy data:

use Faker\Factory;

public function run()
{
    $faker = Factory::create();

    for ($i = 0; $i < 10; $i++) {
        $data = [
            'title'   => $faker->sentence(5),
            'content' => $faker->paragraph(3),
        ];

        $this->db->table('posts')->insert($data);
    }
}

📌 Running All Seeders Together

If we have multiple seeders and want to run them all through a single command:

>> Create a DatabaseSeeder:

php spark make:seeder DatabaseSeeder

>> Then call other seeders within it:

public function run()
{
    $this->call('UserSeeder');
    $this->call('PostSeeder');
}

>> Run:

php spark db:seed DatabaseSeeder

📌 Seeding via Migrations (Optional)

We can also trigger seeding at the end of a migration if needed:

public function up()
{
    // Migration code...

    $seeder = \Config\Services::seeder();
    $seeder->call('PostSeeder');
}

📌 What are Best Practices for Using Seeders?

  • Keep our seeders organized per table or data type.
  • Use Faker for generating realistic dummy data.
  • Avoid sensitive or live data in seeders for production environments.
  • Use a central DatabaseSeeder to manage all seeders.
  • Document our seeders for easier collaboration.

📌 Conclusion

Seeders in CodeIgniter 4 are a powerful way to automate our database setup during development and testing. They simplify the process of populating tables with sample data and ensure consistency across environments.

Whether we need to quickly fill our database with test posts, users, or product listings seeders save time and prevent errors.