$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[January 10, 2026] Laravel

Laravel Database Seeding: Factories Done Right

Laravel Database Seeding: Factories Done Right

────────────────────────────────────────────────────────

Laravel Database Seeding: Factories Done Right

Tip: Factory States for Variants

public function published(): static
{
    return $this->state(fn() => [
        'published' => true,
        'published_at' => now(),
    ]);
}

Usage: Post::factory()->published()->create().

Gotcha: Factory Relationships Need for()

Post::factory()->for(Category::factory())->create();

Creates both the category and the post in one call.

Tip: has() for Related Models

Post::factory()->hasComments(5)->create();

Creates a post with 5 comments automatically.

Gotcha: sequence() for Incrementing Values

Post::factory()
    ->count(10)
    ->state(new Sequence(
        ['position' => 1],
        ['position' => 2],
    ))
    ->create();

Tip: Faker Methods You Might Not Know

fake()->word();
fake()->sentence(3);
fake()->boolean(75);

Gotcha: afterCreating Callbacks

Post::factory()
    ->afterCreating(fn($post) => $post->tags()->attach(Tag::inRandomOrder()->limit(3)->get()))
    ->create();

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Model factories are a productivity multiplier, but they become a maintenance burden when they don't reflect real data shapes. I've adopted the practice of using factory states extensively — active(), vip(), withPendingOrders() — to make test data explicit. A factory that produces random gibberish is worse than no factory at all. Also, never seed production data with factories; use proper database migrations and backups.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts