Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Created July 15, 2024 10:20
Show Gist options
  • Save vielhuber/0ae74eb1847eb7b8d52ac6128940bfba to your computer and use it in GitHub Desktop.
Save vielhuber/0ae74eb1847eb7b8d52ac6128940bfba to your computer and use it in GitHub Desktop.
factories and seeders #laravel
<?php
namespace Database\Factories;
use App\Models\X;
use App\Models\Y;
use App\Models\Z;
use Illuminate\Database\Eloquent\Factories\Factory;
class TestFactory extends Factory
{
public function definition(): array
{
return [
'foo' => fake()->unique()->regexify('[0-9][0-9][0-9][0-9][0-9][\-][0-9][0-9]'),
'bar' => fake()->optional()->realText(200),
'x_id' => X::factory(), // this is how to handle relationships
'y_id' => Y::factory(),
'z_id' => Z::factory(),
];
}
}
<?php
namespace Database\Seeders;
use App\Models\Test;
use Illuminate\Database\Seeder;
class TestSeeder extends Seeder
{
public function run(): void
{
Test::factory()
->count(10)
->create();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment