PHP code example of marwen-brini / smart-seeder

1. Go to this page and download the library: Download marwen-brini/smart-seeder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marwen-brini / smart-seeder example snippets




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

public function data(): array
{
    return [
        ['name' => 'Product 1', 'price' => 99.99],
        ['name' => 'Product 2', 'price' => 149.99],
    ];
}

public function data(): array
{
    return $this->fake(10, [
        'price' => fn($faker) => $faker->randomFloat(2, 10, 500),
    ]);
}

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' => '    'email' => 'rt', 'skip', 'log'

protected array $dependencies = [
    CategoriesSeeder::class,
    BrandsSeeder::class,
];

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}");
}

protected function transform(array $record): array
{
    $record['slug'] = Str::slug($record['name']);
    return $record;
}

protected array $mutators = [
    'password' => 'hash',           // Hash passwords
    'email' => 'lowercase',         // Lowercase emails
    'name' => 'trim',               // Trim whitespace
];

protected array $environments = ['local', 'staging'];



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' => '      // AutoFaker generated
            ...$this->fake(5, ['sku' => fn($f) => 'AUTO-' . $f->unique()->randomNumber(5)]),
        ];
    }
}
bash
php artisan vendor:publish --tag="smart-seeder-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="smart-seeder-config"
bash
php artisan seed:make ProductsSeeder
bash
php artisan seed:plan ProductsSeeder
bash
php artisan seed:safe ProductsSeeder
bash
php artisan seed:safe ProductsSeeder --with-dependencies
bash
php artisan seed:plan ProductsSeeder
php artisan seed:plan ProductsSeeder --limit=20
bash
php artisan seed:undo
php artisan seed:undo --seeder=ProductsSeeder
php artisan seed:undo --batch=5
bash
php artisan seed:status
php artisan seed:status --limit=20
bash
php artisan seed:make ProductsSeeder
php artisan seed:make ProductsSeeder --table=products