The ExportableSeeder Trait
The ExportableSeeder Trait
The ExportableSeeder trait is the core of OI Laravel Seeds. It adds export and import functionality to any Laravel Seeder class.
Using the Trait
Add the trait to your seeder class:
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
use OiLab\OiLaravelSeeds\Traits\ExportableSeeder;
class UserSeeder extends Seeder
{
use ExportableSeeder;
// ... trait configuration below
}Required Properties
$jsonFilename
The path(s) to the JSON file(s) containing seed data, relative to the configured storage path.
- Type:
stringorarray - Required: Yes
For a single model:
protected string $jsonFilename = 'users.json';For multiple models:
protected array $jsonFilename = ['users.json', 'profiles.json'];$modelClass
The Eloquent model class(es) to import data into.
- Type:
stringorarray - Required: Yes
For a single model:
protected string $modelClass = User::class;For multiple models:
protected array $modelClass = [User::class, Profile::class];When using arrays, both arrays must have the same number of elements, and they are paired by index.
Optional Properties
$uniqueBy
The column(s) used to determine if a record should be updated or created.
- Type:
stringorarray - Default:
id(or the configureddefault_unique_by)
For a single unique column:
protected string $uniqueBy = 'email';For composite unique keys:
protected array $uniqueBy = ['tenant_id', 'email'];This value is passed directly to updateOrCreate().
$dependencies
An array of seeder class names that must run before this seeder.
- Type:
array - Default:
[]
protected array $dependencies = [
TenantSeeder::class,
RoleSeeder::class,
];This enables automatic dependency ordering via topological sort.
$exportRelations
An array of relation method names to include when exporting data.
- Type:
array - Default:
[]
protected array $exportRelations = ['posts', 'comments'];Relations are only exported when using seed:export --with-relations.
Required Methods
run()
The run() method must call $this->importData() to import seed data:
public function run(): void
{
$this->importData();
}Complete Example
Here's a complete seeder class demonstrating all features:
<?php
namespace Database\Seeders;
use App\Models\Role;
use App\Models\User;
use Illuminate\Database\Seeder;
use OiLab\OiLaravelSeeds\Traits\ExportableSeeder;
class UserSeeder extends Seeder
{
use ExportableSeeder;
protected string $jsonFilename = 'users.json';
protected string $modelClass = User::class;
protected string $uniqueBy = 'email';
protected array $dependencies = [
RoleSeeder::class,
];
protected array $exportRelations = ['roles'];
public function run(): void
{
$this->importData();
}
protected function getExportableAttributes(mixed $model): array
{
return [
'id',
'name',
'email',
'created_at',
'updated_at',
];
}
}Trait Methods
importData(): void
Reads the JSON file(s) and imports records into the database using updateOrCreate().
Called from your run() method:
public function run(): void
{
$this->importData();
}exportData(bool $withRelations = false): int
Exports records from the database to JSON file(s). Returns the number of records exported.
Called via the seed:export command, or directly in tests:
$exported = $this->exportData();
$exported = $this->exportData(withRelations: true);getExportableAttributes(mixed $model): array
Customize which attributes are included in exports. Override this method to filter sensitive data.
Default behavior exports all model attributes:
protected function getExportableAttributes(mixed $model): array
{
return $model->getAttributes();
}See Exporting Data for more details.
Next Steps
- Exporting Data — Learn how to export and customize exports
- Importing Data — Understand the upsert logic
- Seeder Dependencies — Manage complex seeder orderings