Elentra API
8.0
Search
K

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.
1
abstract class RawSeedData implements RawSeedDataInterface
2
{
3
...
4
/**
5
* @var string The database table name
6
*/
7
protected string $table;
8
9
...
10
}

SeedData abstract class

The SeedData class uses an Eloquent model to determine the table structure.
1
abstract class SeedData implements SeedDataProvider
2
{
3
/**
4
* Get the class name of the Eloquent model
5
*
6
* @return string
7
*/
8
abstract public function type(): string;
9
}
Implement the type() method in the seed data class to return the model class name.
Example from RawSeedData
1
class CommunityPagesSeedData extends RawSeedData
2
{
3
/**
4
* @var string The database table name
5
*/
6
protected string $table = 'community_pages';
7
8
/**
9
* Get the community pages list
10
*
11
* @return array
12
*/
13
public static function getCommunityPages(): array
14
{
15
return [
16
[
17
'cpage_id' => 1,
18
'community_id' => 1,
19
'parent_id' => 0,
20
'page_order' => 0,
21
'page_type' => 'default',
22
'menu_title' => 'Home',
23
'page_title' => 'Home',
24
'page_url' => '',
25
'page_content' => '',
26
'copied_from' => 0,
27
'page_active' => 1,
28
'page_visible' => 1,
29
'allow_member_view' => 1,
30
'allow_troll_view' => 1,
31
'allow_public_view' => 1,
32
'updated_date' => Carbon::create("Thu Dec 02 2021")->timestamp,
33
'updated_by' => 1,
34
],
35
];
36
}
37
38
/**
39
* Get the raw seed data
40
*
41
* @return array
42
*/
43
public function data(): array
44
{
45
return self::getCommunityPages();
46
}
47
}
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
1
class CommunityPagesSeedData extends SeedData
2
{
3
/**
4
* Get the community pages list
5
*
6
* @return array
7
*/
8
public static function getCommunityPages(): array
9
{
10
return [
11
[
12
'cpage_id' => 1,
13
'community_id' => 1,
14
'parent_id' => 0,
15
'page_order' => 0,
16
'page_type' => 'default',
17
'menu_title' => 'Home',
18
'page_title' => 'Home',
19
'page_url' => '',
20
'page_content' => '',
21
'copied_from' => 0,
22
'page_active' => 1,
23
'page_visible' => 1,
24
'allow_member_view' => 1,
25
'allow_troll_view' => 1,
26
'allow_public_view' => 1,
27
'updated_date' => Carbon::create("Thu Dec 02 2021")->timestamp,
28
'updated_by' => 1,
29
],
30
];
31
}
32
33
/**
34
* Get the raw seed data
35
*
36
* @return array
37
*/
38
public function data(): array
39
{
40
return self::getCommunityPages();
41
}
42
43
/**
44
* Get the Eloquent model type
45
*
46
* @return string
47
*/
48
public function type(): string
49
{
50
return CommunityPages::class;
51
}
52
53
}
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
- app
- Modules
- Absences
- Database
- Seeds
- Data
- AbsencesSeedData.php
- 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.
1
namespace Entrada\Modules\{MODULE_NAME}\Database\Seeds;
2
3
use Entrada\Modules\{MODULE_NAME}\Database\Seeds\Data;
4
use Entrada\Support\Database\Seeder;
5
6
class {MODULE_NAME}Seeder extends Seeder
7
{
8
public function seeds(): array
9
{
10
return [
11
new Data\{SEED_DATA_CLASS_NAME}(),
12
];
13
}
14
}
  • Replace {MODULE_NAME} with the modules name
  • Replace {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

1
<?php
2
3
namespace Entrada\Modules\{MODULE_NAME}\Providers;
4
5
use Entrada\Modules\{MODULE_NAME}\Database\Seeds\{MODULE_NAME}Seeder;
6
use Entrada\Support\Providers\DatabaseServiceProvider as ServiceProvider;
7
8
class DatabaseServiceProvider extends ServiceProvider
9
{
10
/**
11
* @var array Registered seeders
12
*/
13
protected array $seeders = [
14
'{MODULE_NAME}' => [{MODULE_NAME}Seeder::class],
15
];
16
}
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

class ModuleServiceProvider extends ServiceProvider
{
/**
* Register the module services.
*
* @return void
*/
public function register()
{
$this->app->register(DatabaseServiceProvider::class);
...

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.
1
Carbon::now()->getTimestamp()
Carbon can also be used to create a timestamp from a date string.
1
$newYear = Carbon::create('first day of January 2016');
2
// DateTime example
3
$updated_date = Carbon::create("Dec 09 2019 00:00:00"); // or
4
$updated_date = Carbon::create("2019-12-09 00:00:00")
5
// Date example
6
$updated_date = Carbon::create("Dec 09 2019");
Use timestamp property of Carbon objects to store the actual timestamp in seed data records.
class EventAttendanceSeedData extends RawSeedData
// ...
public function data(): array
{
return [
[
'eattendance_id' => 1,
'event_id' => 2,
'proxy_id' => 3,
'active' => 1,
'comments' => null,
'update_method' => null,
'ea_status_id' => 2,
'updated_date' => Carbon::now()->timestamp,
'updated_by' => 1,
'attendance_time' => Carbon::now()->timestamp,
]
];
}
}