nomanurrahman / fresh-seed
seed and refresh every single table
Package info
pkg:composer/nomanurrahman/fresh-seed
Requires
- php: ^8.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5|^10.0|^11.0
README
A lightweight Laravel package that allows you to truncate and re-seed database tables surgically. This is perfect for development when you need to refresh specific data without running a full migrate:fresh --seed which wipes your entire database.
Features
- π― Surgical Refresh: Truncate/delete only the tables you specify.
- π Multi-Table Support: Refresh multiple tables at once (e.g.,
users,posts). - π Table Presets/Groups: Define curated groups of tables in the config to refresh together.
- πΊοΈ Interactive Selection: Run the command without arguments to select tables from an interactive console prompt.
- π Dry-Run Preview: Preview what actions would be performed without actually modifying database records.
- π Custom Database Connections: Execute the operations against a non-default connection.
- π‘οΈ Constraint Safety & Safe Deletion: Disables foreign key constraints during the process, with an optional
--safemode utilizingDELETEinstead ofTRUNCATEto support transactions and avoid implicit database commits. - βοΈ Auto-Seeder Detection: Automatically locates appropriate seeders using standard Laravel conventions.
Installation
Install the package via composer as a development dependency:
composer require --dev nomanurrahman/fresh-seed
Configuration (Optional)
To configure presets/groups or default connections, publish the configuration file:
php artisan vendor:publish --provider="Nomanurrahman\FreshSeed\FreshSeedServiceProvider" --tag="config"
This will create a config/fresh-seed.php file:
return [ /* * Define presets or groups of tables that can be refreshed together. * Groups can be an array of table names (auto-guesses seeders) or key-value pairs of table => seeder. */ 'groups' => [ 'auth' => [ 'users' => 'Database\\Seeders\\UserSeeder', 'roles' => 'Database\\Seeders\\RoleSeeder', 'permissions' => 'Database\\Seeders\\PermissionSeeder', ], 'shop' => ['products', 'categories', 'orders'], ], /* * Default connection to use if not specified in command. */ 'connection' => null, ];
Usage
1. Interactive Selection
Run the command without options to select which table(s) to refresh interactively:
php artisan fresh:seed
2. Basic Single-Table Refresh
Refresh a single table and let the package locate the matching seeder automatically:
php artisan fresh:seed --table=users
Note: For safety, the command asks for confirmation if run in a production environment. You can bypass this with the
--forceflag.
3. Multi-Table Refresh
Refresh multiple tables sequentially using a comma-separated list:
php artisan fresh:seed --table=users,posts,comments
4. Running Table Groups
Refresh pre-configured groups defined in config/fresh-seed.php:
php artisan fresh:seed --group=auth
5. Specifying a Custom Seeder
Explicitly define a custom seeder class (only valid when refreshing a single table):
php artisan fresh:seed --table=users --seeder=CustomUserSeeder
6. Safe Deletions
Use DELETE instead of TRUNCATE to avoid implicit database commits (useful for maintaining database transaction integrity):
php artisan fresh:seed --table=posts --safe
7. Dry-Run Mode
Preview the execution path without modifying database records:
php artisan fresh:seed --group=auth --dry-run
8. Targeting a Custom Connection
Run the refresh against a secondary or tenant database connection:
php artisan fresh:seed --table=users --database=tenant_1
How Seeder Detection Works
If you don't provide a --seeder option or group mapping, the package looks for seeders in the following order:
StudlyCaseTableNameTableSeeder(e.g.,UsersTableSeeder)StudlyCaseSingularTableNameSeeder(e.g.,UserSeeder)
Checks are conducted in both the root namespace and the standard Database\Seeders\ namespace.
Testing
The package includes a comprehensive test suite. You can run the tests using:
composer test
Security
If you discover any security-related issues, please email nomanurrahman@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.