Пример фабрики и сидера с предустановленными значениями
class CityFactory extends Factory
{
protected $model = City::class;
public function definition()
{
$tz = array_keys(Timezone::getData());
return [
'name' => $this->faker->city,
'timezone' => $this->faker->randomElement($tz),
'enabled' => true,
];
}
}
use Illuminate\Database\Eloquent\Factories\Sequence;
class CitySeeder extends Seeder
{
public function run()
{
$predefinedCities = array_reverse($this->getPredefinedCities());
City::factory(CITY_COUNT)
->state(new Sequence(function () use (&$predefinedCities) {
return array_pop($predefinedCities) ?? [];
}))
->create();
}
private function getPredefinedCities()
{
return [
[
'name' => 'Moscow',
'timezone' => '+0300',
'enabled' => true,
],
[
'name' => 'Saint-Petersburg',
'timezone' => '+0300',
'enabled' => true,
],
[
'name' => 'Sochi',
'timezone' => '+0300',
'enabled' => true,
],
];
}
}