Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Allow seeders to be called only once #39812

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Illuminate/Database/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -53,6 +60,8 @@ public function call($class, $silent = false, array $parameters = [])
if ($silent === false && isset($this->command)) {
$this->command->getOutput()->writeln("<info>Seeded:</info> {$name} ({$runTime}ms)");
}

static::$called[] = $class;
}

return $this;
Expand Down Expand Up @@ -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.
*
Expand Down