Seeders
Definition
Seeders (are a short name for Database Seeders).
Seeders are classes made to seed the database with real data, this data usually should exist in the Application after the installation (Example: the default Users Roles and Permissions or the list of Countries).
Principles
- Seeders SHOULD be created in the Containers. (If the container is using a package that publishes a Seeder class, this class should be manually placed in the Container that make use of it. Do not rely on the package to place it in its right location).
Rules
-
Seeders should be in the right directory inside the container to be loaded.
-
To avoid any conflict between containers seeders classes, you SHOULD always prepend the Seeders of each container with the container name. (Example:
UserPermissionsSeeder
,ItemPermissionsSeeder
).
If 2 seeders classes have the same name but live in different containers, one of them will not be loaded. In these situations you can also prepend the seeder name with the section name
- If you wish to order the seeding of the classes, you can just append
_1
,_2
to your classes.
Folder Structure
- App
- Containers
- {section-name}
- {container-name}
- Data
- Seeders
- ContainerNameRolesSeeder_1.php
- ContainerNamePermissionsSeeder_2.php
- ...
Code Samples
Demo Seeder
class DemoSeeder_1 extends Seeder
{
public function run()
{
app(CreateRoleTask::class)->run('admin', 'Administrator', 'Administrator Role', 999);
// ...
}
}
Same Seeder class is allowed to contain seeding for multiple Models
.
Run the Seeders
After registering the Seeders
you can run this command:
php artisan db:seed
Migrate & seed at the same time
php artisan migrate --seed
Testing Seeder Command
It's useful sometimes to create a big set of testing data. apiato facilitates this task:
- Open
app/Ship/Seeders/SeedTestingData.php
and write your testing data here. - Run this command any time you want this data available (example at staging servers):
php artisan apiato:seed-test
More info at Laravel Docs.