From 0d33f7066e23db7cf9f54a21fb5c0e865768abb5 Mon Sep 17 00:00:00 2001 From: Sebastian De Deyne Date: Mon, 29 Nov 2021 12:45:03 +0100 Subject: [PATCH] Add callOnce to Seeder --- src/Illuminate/Database/Seeder.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Illuminate/Database/Seeder.php b/src/Illuminate/Database/Seeder.php index 441fa27d6c16..560e81c15211 100755 --- a/src/Illuminate/Database/Seeder.php +++ b/src/Illuminate/Database/Seeder.php @@ -23,6 +23,13 @@ abstract class Seeder */ protected $command; + /** + * Seeders that have been called at least one time. + * + * @var array + */ + protected static $called = []; + /** * Run the given seeder class. * @@ -53,6 +60,8 @@ public function call($class, $silent = false, array $parameters = []) if ($silent === false && isset($this->command)) { $this->command->getOutput()->writeln("Seeded: {$name} ({$runTime}ms)"); } + + static::$called[] = $class; } return $this; @@ -82,6 +91,22 @@ public function callSilent($class, array $parameters = []) $this->call($class, true, $parameters); } + /** + * Run the given seeder class once. + * + * @param array|string $class + * @param bool $silent + * @return void + */ + public function callOnce($class, $silent = false, array $parameters = []) + { + if (in_array($class, static::$called)) { + return; + } + + $this->call($class, $silent, $parameters); + } + /** * Resolve an instance of the given seeder class. *