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