After this lesson you can insert known fictional books with a seeder and recognize when repeating the operation would create duplicates. A seeder prepares data. It is useful for a reproducible demonstration, but it is not the same thing as a schema migration or a backup restoration procedure.
Choose data that teaches the feature
Paper Lantern Bookshop begins with The Paper Orchard and A Map of Quiet Streets. These are invented catalogue titles used only in the course. They are not claims about published books or this site's customers. Keeping a small predictable dataset makes it easier to see whether lists, details and tests agree.
Create app/Database/Seeds/BookSeeder.php with this class after applying the books migration.
<?php
namespace App\Database\Seeds;
use CodeIgniter\Database\Seeder;
class BookSeeder extends Seeder
{
public function run()
{
$this->db->table('books')->insertBatch([
['title' => 'The Paper Orchard'],
['title' => 'A Map of Quiet Streets'],
]);
}
}The class was linted and executed against the temporary SQLite database. It uses the connection's query builder to insert two rows. The database generates the identifiers. We do not hard-code a production identifier or assume that every existing database starts numbering at one.
The official Database Seeding guide documents Seeder classes and the db:seed command. The choice of fictional titles and the simple insert-only behavior are ours. Read the seeder before running it: its name does not tell you whether it inserts, updates or deletes data.
Run it once on the fresh schema
php spark db:seed BookSeederExample output captured on 2026-11; yours will differ. The banner is omitted.
Seeded: App\Database\Seeds\BookSeederThis output confirms that the seeder ran without reporting a failure. It does not prove the intended records are visible on a page yet. Later lessons query them through the model and verify the response content. A good verification chain connects the setup operation with a concrete observable result.
On the fresh learning database, the two records receive identifiers used in our demonstrations. If you have already inserted other records or rerun the seeder, your identifiers and row count can differ. Do not “fix” that by deleting an unrelated database. Either inspect your actual rows or rebuild only your disposable learning workspace deliberately.
Repeatable does not always mean idempotent
An operation is idempotent when repeating it has the same relevant effect as doing it once. This seeder is not idempotent: every run inserts another pair of rows. The migration runner tracks applied schema versions; a direct db:seed invocation does not use that migration history to skip these inserts.
For a real reference-data task, you might design a stable unique key and an update-or-insert policy. That requires deciding which data may be replaced and how uniqueness is enforced. This course deliberately avoids pretending that duplicate titles are necessarily invalid: two different books can share a title, so title alone is not a carefully designed identity key.
Do not add a blanket delete to make a seeder appear repeatable unless removing all existing records is explicitly intended. In a disposable test database, resetting data can be appropriate. In an ordinary application database, the same action could erase work. Context determines whether a reset is safe.
Separate demonstration setup from real operations
Seeders can support local development and automated tests. They should not automatically run every time somebody opens the catalogue page. A GET request must not silently append demonstration data. Keep setup commands in the CLI workflow and application reads in controllers and models.
Tests need known state too. The testing lesson configures the test framework to prepare the schema and seed its own isolated database. That arrangement allows each check to make assertions about known fictional records without depending on whatever you last entered through a local form.
The fetched official Database Seeding guide explains the framework mechanism. Our seeder contains no personal information and does not import anything from the repository's application. If you adapt it, keep demonstrations free of copied customer data, credentials and private identifiers.
Make assumptions visible
Record whether your current learning database is fresh, seeded once or modified by form submissions. This simple note prevents confusion when a list has more books than the lesson screenshot or output excerpt. A difference in data state is not automatically a framework regression.
When reporting a problem, describe the seed command and whether it was repeated. Saying “the catalogue has duplicates after I seeded twice” immediately identifies the operation to inspect. Saying “CodeIgniter duplicated my books” hides the relevant cause and encourages investigation in the wrong layer.
Common mistakes
Expecting a seeder to be skipped because a migration already ran confuses two systems. Running it inside a request causes repeated writes. Using real customer records for demonstration creates unnecessary privacy risk. Another mistake is making title a unique identity without considering the domain. Finally, adding a destructive delete merely to hide duplicates changes the operation's meaning and needs an explicit decision.
Exercise
Predict the number of inserted rows after running this seeder twice on a fresh books table. Explain why that differs from running migrate twice, then describe how automated tests can avoid depending on your manually changed local catalogue.
Show an answer
Two runs insert four rows because the class performs two inserts each time. The migration system tracks applied schema versions and does not recreate the same table on every invocation. Tests should prepare their own disposable database with a known schema and seed data rather than reuse the ordinary learning database.
Previous lesson · Next lesson · Related: explicit routes · Related: security boundaries
Key points
- The seeder inserts two fictional titles per run.
- Running this seeder repeatedly creates additional records.
- Tests should prepare their own known data state.