Factories and Seeders
php naluz make:factory PostFactory php naluz make:seeder PostSeederphp naluz db:seed [--class=Database\Seeders\DatabaseSeeder] php naluz migrate:fresh --seedfinal class PostFactory extends Factory{ protected string $model = Post::class;
public function definition(): array { return [ 'user_id' => UserFactory::new(), // nested factories are created and their id used 'title' => ucfirst($this->fake()->words(4)), 'body' => $this->fake()->paragraph(), ]; } public function published(): static { return $this->state(['published' => true]); }}
// models: `use HasFactory;`User::factory()->create(); // persisted ->make() = not persistedPost::factory(5)->published()->create(['user_id' => 1]); // 5 rows with overridesPost::factory()->state(fn (array $a) => ['title' => strtoupper($a['title'])])->afterCreating(fn ($p) => …)->create();
// database/seeders/DatabaseSeeder.phppublic function run(): void { User::factory(10)->create(); $this->call([PostSeeder::class]); }Naluz\Support\Fake is a small built-in generator (names, emails, words/sentences, uuid, dates, numbers…);
Fake::seed(42) makes data reproducible. Factories use forceFill() (trusted code, $fillable doesn’t apply).
migrate:fresh and db:seed refuse to run in production without --force.