This library allows you to run your Laravel seeders only once.
No matter how many times artisan command db:seed is called. Done seeders will never be executed again.
Works similarly to migrations. First creates table in database, then logs all seeds that extend abstract class Kdabrow\SeederOnce\SeederOnce. In nutshell this will prevent to execute method run() if was executed in the past. Mechanism of logging data into database is heavily inspired by Laravel migration mechanism from package illuminate/database.
By composer:
| php | laravel / lumen | seeder-once |
|---|---|---|
| 8.3, 8.4 | 13.0 | composer require "kdabrow/seeder-once: ^6.1" |
| 8.2, 8.3, 8.4 | 10.0, 11.0, 12.0 | composer require "kdabrow/seeder-once: ^6.1" |
| 8.2, 8.3, 8.4 | 12.0 | composer require "kdabrow/seeder-once: ^6.0" |
| 8.2, 8.3 | 10.0, 11.0 | composer require "kdabrow/seeder-once: ^5.0" |
| 8.1, 8.2 | 10.0 | composer require "kdabrow/seeder-once: ^4.0" |
| 8.0, 8.1 | 9.21 | composer require "kdabrow/seeder-once: ^3.0" |
| 7.3, 7.4, 8.0 | 8.0, 9.0 | composer require "kdabrow/seeder-once: ^2.2" |
| 7.2, 7.3, 7.4, 8.0 | 6.0, 7.0 | composer require "kdabrow/seeder-once: ^1.2" |
In Lumen do not forget to register provider in bootstrap/app.php
$app->register(Kdabrow\SeederOnce\Providers\SeederOnceProvider::class);Use this command:
php artisan db:installThis step is optional. Trait SeederOnce detects if table exists. If not, creates it automatically.
So result should look like this:
use Kdabrow\SeederOnce\SeederOnce;
class SomeSeeder extends SeederOnce
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
//
}
}This prevents to seed class SomeSeeder if was already seeded before.
Tip: Always replace class Seeder with SeederOnce. Otherwise, db:seed command might print unexpected results.
It is possible to overwrite default functionality by change parameter $seedOnce to false:
use Kdabrow\SeederOnce\SeederOnce;
class DatabaseSeeder extends SeederOnce
{
public bool $seedOnce = false;
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
//
}
}Such seeder will run like normal laravel seeder (as many times as called).
It is possible to publish configuration file:
php artisan vendor:publish --tag=seederonce.configDefault table name is: seeders
By default, seeder-once changes output of db:seed console command. Seeders that were run in the past will not be printed in the command output. It's possible to change that behaviour in configuration or by env variable:
SEEDER_ONCE_PRINT_OUTPUT=trueOutput for already run seeders looks like this:
Database\Seeders\SomeSeeder was already seeded ..................... DONE