Seeding the database in BowPHP
Introduction​
Seeding is a mechanism that lets you hydrate your database so you can use it during development. In Bow, a seeder is associated with a table when it is created.
Adding a seeder​
To add a seeder, use php bow with the add:seeder command followed by the table name.
php bow add:seeder pets
A pets_seeder.php file will be created in the seeders folder. Here is its content:
/**
* The pets seeder
*
* @see https://github.com/fzaninotto/Faker for all documentation
*/
$faker = \Faker\Factory::create();
$seeds['pets'] = [];
foreach (range(1, 5) as $key) {
$seeds['pets'][] = [
'name' => $faker->name,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
}
return $seeds;
The
petskey is the name of the table being seeded.
Running the seeding​
To run the seeding, use the seed:table command followed by the table name.
php bow seed:table pets
You can also run all seeders with the seed:all command. This will seed all the files in seeders.
php bow seed:all
Helper​
You will often be tempted to run seeders while testing your application. The db_seed helper is very handy and lets you seed data right in the middle of a test, for example.
db_seed("user", $overrides = ["name" => "Franck"]);
With $overrides you can modify the values of the seeding.
Is something missing?
If you run into problems with the documentation or have suggestions to improve the documentation or the project in general, please open an issue for us, or send a tweet mentioning the Twitter account @bowframework or directly on github.