PHP

Laravel Faker: Generate Fake Data Easily (with Examples)

Updated

Laravel Faker generating fake data

Faker is already installed in a new Laravel app, under the name fakerphp/faker, and you reach it through the fake() helper:

$name  = fake()->name();
$email = fake()->unique()->safeEmail();
$year  = fake()->numberBetween(1900, 2026);

Do not run composer require fzaninotto/faker. Packagist reports that package as abandoned, its last release was v1.9.2 on 2020-12-11, and its GitHub repository is archived. The maintained fork is fakerphp/faker; a fresh composer create-project laravel/laravel pulled v1.24.1 in August 2026 and never mentioned the old name. Packagist’s abandoned flag, the require-dev entry and the formatter, factory and provider examples below are checked by a script that builds a throwaway Laravel app with composer create-project and deletes it on exit.

It is a dev dependency, and that matters

Faker sits in require-dev. Run a production install and it goes away:

$ composer install --no-dev --dry-run
  - Removing fakerphp/faker (v1.24.1)

The fake() helper is defined conditionally, in Illuminate/Foundation/helpers.php:

if (! function_exists('fake') && class_exists(\Faker\Factory::class)) {
    function fake($locale = null): \Faker\Generator
    { ... }
}

So fake() does not throw a helpful error in production — it does not exist. A seeder, a console command, or a “demo data” route that calls it will fatal on the first deploy that runs --no-dev. Keep Faker inside database/factories and tests/ — and note that a factory only describes a record, so something still has to call it, which is what a seeder is for.

Factories, as they actually work now

A $factory->define(...) block and factory(App\User::class, 50) calls are from Laravel 7 and earlier. Probing a live Laravel 13 application:

factory_helper=no
app_user_class=no
app_models_user_class=yes
fake_helper=yes
fake_returns=Faker\Generator

A factory is a class, generated with php artisan make:factory WidgetFactory (or make:model Widget -mf for the model, migration and factory together):

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class WidgetFactory extends Factory
{
    public function definition(): array
    {
        return [
            'name'  => fake()->words(2, true),
            'sku'   => fake()->unique()->bothify('??-####'),
            'price' => fake()->numberBetween(100, 9999),
        ];
    }

    public function cheap(): static
    {
        return $this->state(fn (array $attributes) => [
            'price' => fake()->numberBetween(1, 99),
        ]);
    }
}

The model opts in with use HasFactory;. Real output from that factory:

Widget::factory()->count(3)->make()               -- 3 models, never touches the database
Widget::factory()->count(3)->create()             -- 3 rows inserted
Widget::factory()->make(['name' => 'Fixed'])      -- override_works=yes
Widget::factory()->count(5)->cheap()->make()      -- state_applies=yes (every price < 100)
Widget::factory()->count(4)->sequence(
    ['sku' => 'AA-0001'], ['sku' => 'BB-0002'])   -- AA-0001,BB-0002,AA-0001,BB-0002

make() versus create() is the distinction worth internalising: make() builds unsaved models, so it works in tests with no database at all. sequence() is how you get deterministic variety without writing a loop.

Formatters: call them, don’t read them

The $faker->name style still works and tells you not to:

Since fakerphp/faker 1.14: Accessing property "name" is deprecated, use "name()" instead.

Misspell a formatter and you get a clear failure rather than a null:

InvalidArgumentException: Unknown format "notARealFormatter"

Two modifiers are worth knowing precisely. unique() guarantees distinct values and then gives up loudly — five calls to unique()->numberBetween(1, 5) returned five distinct numbers, and the sixth threw:

OverflowException: Maximum retries of 10000 reached without finding a unique value

That is a genuine failure mode in a large seeder: a unique()->numberBetween(1, 100) in a factory used 200 times will blow up rather than silently repeat. optional(0.5) returns null about half the time — 517 nulls in 1,000 calls in the run above — which is how you exercise nullable columns.

Locale

fake() takes a locale, and the default comes from config:

fake()->name();            // en_US, from config('app.faker_locale')
fake('fr_FR')->name();     // Jeanne Michel
fake('de_DE')->address();  // Ernst-Kuhn-Straße 8 / 26652 Melle

Set APP_FAKER_LOCALE in .env to change the default for the whole app; config/app.php reads it as 'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'). Locales are partial — an unsupported formatter falls back to en_US rather than failing.

Custom providers

A custom provider still works:

final class IsbnProvider extends \Faker\Provider\Base
{
    public function isbn13(): string
    {
        $body = static::randomElement(['978', '979']).static::numerify('#########');
        $sum = 0;
        foreach (str_split($body) as $i => $digit) {
            $sum += (int) $digit * ($i % 2 === 0 ? 1 : 3);
        }

        return $body.((10 - $sum % 10) % 10);
    }
}

fake()->addProvider(new IsbnProvider(fake()));
fake()->isbn13();   // 9785903853373 - 13 digits, check digit recomputed and verified

Register it once — in a service provider’s boot() for app-wide use, or in the test that needs it — and it behaves like any built-in formatter. Because fake() caches one generator per locale in the container, addProvider() called during boot applies everywhere afterwards.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.