Creating Seed Data
Steps in creating a new seed data file
Adding new seed data is as simple as creating a new PHP file with a name matching the entity's name.
Using PortfolioArtifact
model as an example, the file name should be PortfolioArtifactsSeedData.php
Creating Seed Data classes
When creating a new seed data class, extend the SeedData
abstract class. There are two abstract classes that can be used, SeedData
and RawSeedData
.
The seed generator is looking for the data()
method to populate the database using the returned array.
RawSeedData abstract class
For RawSeedData
, the $table
property must be set to the name of the database table.
SeedData abstract class
The SeedData
class uses an Eloquent model to determine the table structure.
Implement the type()
method in the seed data class to return the model class name.
Example from RawSeedData
Using the Carbon library to generate timestamps makes the timestamp easier to read (i.e. instead of using UNIX integer timestamps).
A value is needed for every column in the table when using the RawSeedData.
Example extending SeedData
When extending SeedData
instead of RawSeedData
, extra records may be created if an event gets fired every time the model in the seed data class is created. For example, if the model class has additional code in it's boot()
method, then it may create additional records that could conflict with other seed data files. These will not be created if RawSeedData
is used instead.
When creating seed data that contains an ID which will be used in multiple locations, create a const
for readability.
Example
STUDENT_PROXY_ID
contains the ID of a user that has a role of student
Updating/Adding a Seeder File
After creating a new seed data class, the seeds()
method in the module's corresponding seeder class must be updated. Every module should have a seeder class located in the following path:
app/Modules/{MODULE_NAME}/Database/Seeds
This file's name should match the module name suffixed with Seeder
. For example, the Absences module seeder class file would be:
app/Modules/Absences/Database/Seeds/AbsencesSeeder.php
This seeder class is responsible for importing and initializing all the seed data classes that belong to that module.
If the module doesn't already have a seeder file, create one using the snippet below and other seeder files as an example.
Replace
{MODULE_NAME}
with the modules nameReplace
{SEED_DATA_CLASS_NAME}
with the seed data class name
Registering seeder class
This step is only necessary if you create a new module’s Seeder that didn’t exist before. Once you create a new Seeder, update the module database service provider i.e. app/Modules/{MODULE_NAME}/Providers/DatabaseServiceProvider.php
to include the new seeder.
Example
Replace {MODULE_NAME}
with the module's name.
Registering the database service provider
To register the database service provider, add it to the module service provider (if this was not done already).
Example
Defining timestamps in Seed Data
If seed data needs to include a timestamp relative to today's date, then Laravel's Carbon
class should be used.
Carbon can also be used to create a timestamp from a date string.
Use timestamp
property of Carbon objects to store the actual timestamp in seed data records.
Last updated