Exporting Data
Exporting Data
The exportData() method exports records from your database to JSON files. It's typically called via the seed:export command, but can also be called directly.
Basic Export
public function exportData(bool $withRelations = false): intExport returns the total number of records exported.
Via Command
Export records for a specific seeder:
php artisan seed:export --seeder=UserSeederExport all seeders:
php artisan seed:exportExport with relations included:
php artisan seed:export --seeder=UserSeeder --with-relationsVia Code
In tests or elsewhere, call exportData() directly:
$seeder = new UserSeeder();
$count = $seeder->exportData();Filtering Sensitive Data
By default, all model attributes are exported. Override getExportableAttributes() to remove sensitive fields:
protected function getExportableAttributes(mixed $model): array
{
return [
'id',
'name',
'email',
'email_verified_at',
'created_at',
'updated_at',
];
}This removes password and remember_token from the export.
Complete Example
<?php
namespace Database\Seeders;
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;
public function run(): void
{
$this->importData();
}
protected function getExportableAttributes(mixed $model): array
{
return [
'id',
'name',
'email',
'email_verified_at',
'is_admin',
'created_at',
'updated_at',
];
}
}When you run php artisan seed:export --seeder=UserSeeder, the JSON file will only contain these attributes.
JSON Output Format
The exported JSON file is formatted with pretty printing and unescaped Unicode characters (configured in config/oi-laravel-seeds.php).
Example Output
For the seeder above, storage/app/private/seeders/users.json looks like:
[
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"email_verified_at": "2025-01-01T00:00:00.000000Z",
"is_admin": true,
"created_at": "2025-01-01T00:00:00.000000Z",
"updated_at": "2025-01-01T00:00:00.000000Z"
},
{
"id": 2,
"name": "Jane Smith",
"email": "[email protected]",
"email_verified_at": "2025-01-02T00:00:00.000000Z",
"is_admin": false,
"created_at": "2025-01-02T00:00:00.000000Z",
"updated_at": "2025-01-02T00:00:00.000000Z"
}
]Exporting with Relations
When exporting with relations, related data is nested in the JSON:
Seeder Configuration
protected array $exportRelations = ['posts', 'comments'];Export Command
php artisan seed:export --seeder=UserSeeder --with-relationsExample Output
[
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"created_at": "2025-01-01T00:00:00.000000Z",
"updated_at": "2025-01-01T00:00:00.000000Z",
"posts": [
{
"id": 1,
"title": "First Post",
"content": "Hello world",
"created_at": "2025-01-01T00:00:00.000000Z",
"updated_at": "2025-01-01T00:00:00.000000Z"
}
],
"comments": [
{
"id": 10,
"body": "Great post!",
"created_at": "2025-01-01T00:00:00.000000Z",
"updated_at": "2025-01-01T00:00:00.000000Z"
}
]
}
]Important Notes
- Exporting relations includes the nested data in JSON but does not affect imports. When importing, only the parent model is created/updated.
- Relations must be defined as methods on your model (e.g.,
public function posts(): HasMany). - Use
getExportableAttributes()to filter both parent and relation attributes.
Multiple Models
When a seeder handles multiple models, each gets its own export:
protected array $jsonFilename = ['users.json', 'profiles.json'];
protected array $modelClass = [User::class, Profile::class];The export command exports records for both models to separate files.
Next Steps
- Importing Data — Learn how imports work with the upsert logic
- Configuration — Customize JSON output formatting