marwen-brini/smart-seeder is a Laravel package for this is my package smart-seeder.
It currently has 1 GitHub stars and 0 downloads on Packagist (latest version v1.0.0).
Install it with composer require marwen-brini/smart-seeder.
Discover more Laravel packages by marwen-brini
or browse all Laravel packages to compare alternatives.
Last updated
Safe, intelligent database seeding for Laravel with preview capabilities, idempotent operations, environment awareness, and full rollback support.
seed:planseed:undocomposer require marwen-brini/smart-seeder --dev
Publish and run the migrations for history tracking:
php artisan vendor:publish --tag="smart-seeder-migrations"
php artisan migrate
Optionally publish the config file:
php artisan vendor:publish --tag="smart-seeder-config"
php artisan seed:make ProductsSeeder
<?php
namespace Database\Seeders;
use Filler\Filler;
class ProductsSeeder extends Filler
{
protected array $uniqueBy = ['sku'];
protected string $mode = 'upsert';
public function table(): string
{
return 'products';
}
public function data(): array
{
return [
['sku' => 'LAPTOP-001', 'name' => 'Laptop Pro', 'price' => 999.99],
['sku' => 'MOUSE-001', 'name' => 'Wireless Mouse', 'price' => 49.99],
];
}
}
php artisan seed:plan ProductsSeeder
php artisan seed:safe ProductsSeeder
| Mode | Insert New | Update Existing | Delete Missing |
|------|------------|-----------------|----------------|
| upsert | Yes | Yes | No |
| insert_ignore | Yes | No | No |
| insert_only | Yes | Error | No |
| update_only | No | Yes | No |
| sync | Yes | Yes | Yes |
public function data(): array
{
return [
['name' => 'Product 1', 'price' => 99.99],
['name' => 'Product 2', 'price' => 149.99],
];
}
AutoFaker automatically detects field types and generates appropriate fake data:
public function data(): array
{
return $this->fake(10, [
'price' => fn($faker) => $faker->randomFloat(2, 10, 500),
]);
}
Use existing Laravel factories:
protected ?string $model = Product::class;
public function data(): array
{
return [
...$this->factory(5), // 5 regular products
...$this->factory(3, ['premium']), // 3 with state
...$this->factorySequence([ // specific values
['name' => 'Item A'],
['name' => 'Item B'],
]),
];
}
protected array $rules = [
'name' => 'required|string|min:3',
'price' => 'required|numeric|min:0',
'email' => 'required|email',
];
protected string $onValidationError = 'skip'; // 'abort', 'skip', 'log'
Define seeder dependencies to ensure correct execution order:
protected array $dependencies = [
CategoriesSeeder::class,
BrandsSeeder::class,
];
Run with dependencies:
php artisan seed:safe ProductsSeeder --with-dependencies
Preview what a seeder will do:
php artisan seed:plan ProductsSeeder
php artisan seed:plan ProductsSeeder --limit=20
Execute a seeder safely:
php artisan seed:safe ProductsSeeder
php artisan seed:safe ProductsSeeder --force
php artisan seed:safe ProductsSeeder --with-dependencies
php artisan seed:safe ProductsSeeder --dry-run
php artisan seed:safe --all # Run all Filler seeders
Rollback the last seeder execution:
php artisan seed:undo
php artisan seed:undo --seeder=ProductsSeeder
php artisan seed:undo --batch=5
View seeder history:
php artisan seed:status
php artisan seed:status --limit=20
Generate a new seeder:
php artisan seed:make ProductsSeeder
php artisan seed:make ProductsSeeder --table=products
Run custom logic after records are inserted or updated:
protected function afterInsert(array $record, int $id): void
{
// Called after each record is inserted
// $id contains the auto-incremented ID
cache()->forget('products_count');
}
protected function afterUpdate(array $record, object $existing): void
{
// Called after each record is updated
// $existing contains the original record before update
Log::info("Product updated: {$existing->name}");
}
Transform records before they are inserted:
protected function transform(array $record): array
{
$record['slug'] = Str::slug($record['name']);
return $record;
}
Use mutators for field-level transformations:
protected array $mutators = [
'password' => 'hash', // Hash passwords
'email' => 'lowercase', // Lowercase emails
'name' => 'trim', // Trim whitespace
];
Restrict seeders to specific environments:
protected array $environments = ['local', 'staging'];
<?php
namespace Database\Seeders;
use App\Models\Product;
use Filler\Filler;
class ProductsSeeder extends Filler
{
protected array $uniqueBy = ['sku'];
protected string $mode = 'upsert';
protected string $version = '2.0';
protected ?string $model = Product::class;
protected array $dependencies = [
CategoriesSeeder::class,
];
protected array $environments = ['local', 'staging'];
protected array $rules = [
'sku' => 'required|string',
'name' => 'required|string|min:3',
'price' => 'required|numeric|min:0',
];
protected string $onValidationError = 'skip';
public function table(): string
{
return 'products';
}
public function data(): array
{
return [
// Manual data
['sku' => 'FEATURED-001', 'name' => 'Featured Product', 'price' => 299.99],
// Factory generated
...$this->factory(10),
// AutoFaker generated
...$this->fake(5, ['sku' => fn($f) => 'AUTO-' . $f->unique()->randomNumber(5)]),
];
}
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover a security vulnerability, please send an email to [email protected]. All security vulnerabilities will be promptly addressed.
The MIT License (MIT). Please see License File for more information.