# Creating Seed Data

Adding new seed data is as simple as creating a new PHP file with a name matching the entity's name.

{% hint style="success" %}
Using `PortfolioArtifact` model as an example, the file name should be `PortfolioArtifactsSeedData.php`
{% endhint %}

### 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.

{% code lineNumbers="true" %}

```php
abstract class RawSeedData implements RawSeedDataInterface
{
    ...
    /**
     * @var string The database table name
     */
    protected string $table;
    
    ...
}
```

{% endcode %}

#### SeedData abstract class

The `SeedData` class uses an Eloquent model to determine the table structure.

{% code lineNumbers="true" %}

```php
abstract class SeedData implements SeedDataProvider
{
    /**
     * Get the class name of the Eloquent model
     *
     * @return string
     */
    abstract public function type(): string;
}
```

{% endcode %}

Implement the `type()` method in the seed data class to return the model class name.

**Example from RawSeedData**

{% code lineNumbers="true" %}

```php
class CommunityPagesSeedData extends RawSeedData
{
    /**
     * @var string The database table name
     */
    protected string $table = 'community_pages';

    /**
     * Get the community pages list
     *
     * @return array
     */
    public static function getCommunityPages(): array
    {
        return [
            [
                'cpage_id' => 1,
                'community_id' => 1,
                'parent_id' => 0,
                'page_order' => 0,
                'page_type' => 'default',
                'menu_title' => 'Home',
                'page_title' => 'Home',
                'page_url' => '',
                'page_content' => '',
                'copied_from' => 0,
                'page_active' => 1,
                'page_visible' => 1,
                'allow_member_view' => 1,
                'allow_troll_view' => 1,
                'allow_public_view' => 1,
                'updated_date' => Carbon::create("Thu Dec 02 2021")->timestamp,
                'updated_by' => 1,
            ],
        ];
    }

    /**
     * Get the raw seed data
     *
     * @return array
     */
    public function data(): array
    {
        return self::getCommunityPages();
    }
}
```

{% endcode %}

{% hint style="success" %}
Using the Carbon library to generate timestamps makes the timestamp easier to read (i.e. instead of using UNIX integer timestamps).
{% endhint %}

{% hint style="warning" %}
A value is needed for every column in the table when using the `RawSeedData.`
{% endhint %}

**Example extending SeedData**

{% code lineNumbers="true" %}

```php
class CommunityPagesSeedData extends SeedData
{
    /**
     * Get the community pages list
     *
     * @return array
     */
    public static function getCommunityPages(): array
    {
        return [
            [
                'cpage_id' => 1,
                'community_id' => 1,
                'parent_id' => 0,
                'page_order' => 0,
                'page_type' => 'default',
                'menu_title' => 'Home',
                'page_title' => 'Home',
                'page_url' => '',
                'page_content' => '',
                'copied_from' => 0,
                'page_active' => 1,
                'page_visible' => 1,
                'allow_member_view' => 1,
                'allow_troll_view' => 1,
                'allow_public_view' => 1,
                'updated_date' => Carbon::create("Thu Dec 02 2021")->timestamp,
                'updated_by' => 1,
            ],
        ];
    }

    /**
     * Get the raw seed data
     *
     * @return array
     */
    public function data(): array
    {
        return self::getCommunityPages();
    }

    /**
     * Get the Eloquent model type
     *
     * @return string
     */
    public function type(): string
    {
        return CommunityPages::class;
    }

}
```

{% endcode %}

{% hint style="warning" %}
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.
{% endhint %}

{% hint style="danger" %}
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
{% endhint %}

#### 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:   &#x20;

> `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`

```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.

{% code lineNumbers="true" %}

```php
namespace Entrada\Modules\{MODULE_NAME}\Database\Seeds;

use Entrada\Modules\{MODULE_NAME}\Database\Seeds\Data;
use Entrada\Support\Database\Seeder;

class {MODULE_NAME}Seeder extends Seeder
{
    public function seeds(): array
    {
        return [
            new Data\{SEED_DATA_CLASS_NAME}(),
        ];
    }
}
```

{% endcode %}

{% hint style="info" %}

* Replace `{MODULE_NAME}` with the modules name
* Replace `{SEED_DATA_CLASS_NAME}` with the seed data class name
  {% endhint %}

### Registering seeder class

{% hint style="warning" %}
**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.
{% endhint %}

#### **Example**

{% code lineNumbers="true" %}

```php
<?php

namespace Entrada\Modules\{MODULE_NAME}\Providers;

use Entrada\Modules\{MODULE_NAME}\Database\Seeds\{MODULE_NAME}Seeder;
use Entrada\Support\Providers\DatabaseServiceProvider as ServiceProvider;

class DatabaseServiceProvider extends ServiceProvider
{
    /**
     * @var array Registered seeders
     */
    protected array $seeders = [
        '{MODULE_NAME}' => [{MODULE_NAME}Seeder::class],
    ];
}
```

{% endcode %}

{% hint style="info" %}
Replace `{MODULE_NAME}` with the module's name.
{% endhint %}

### 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

<pre class="language-php"><code class="lang-php">class ModuleServiceProvider extends ServiceProvider
{
<strong>   /**
</strong>   * Register the module services.
   *
   * @return void
   */
   public function register()
   {
      $this->app->register(DatabaseServiceProvider::class);
      ...
</code></pre>

### 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.

{% code lineNumbers="true" %}

```php
 Carbon::now()->getTimestamp()
```

{% endcode %}

Carbon can also be used to create a timestamp from a date string.

{% code lineNumbers="true" %}

```php
$newYear = Carbon::create('first day of January 2016');
// DateTime example
$updated_date = Carbon::create("Dec 09 2019 00:00:00"); // or 
$updated_date = Carbon::create("2019-12-09 00:00:00")
// Date example
$updated_date = Carbon::create("Dec 09 2019");
```

{% endcode %}

Use `timestamp` property of Carbon objects to store the actual timestamp in seed data records.

```php
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,
            ]
        ];
    }
}
```

[Official Carbon documentation](https://carbon.nesbot.com/docs/)

[Article on using Carbon](https://www.digitalocean.com/community/tutorials/easier-datetime-in-laravel-and-php-with-carbon)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.elentra.org/api/automated-testing/seed-data/creating-seed-data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
