Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jrdavidson's avatar

Faker Not Returning Formatter

I'm trying to figure out why when I run a test and use Teacher::factory()->create(); it gives me InvalidArgumentException: Unknown formatter "firstName". I'm not sure why. A little background is that I am

<?php

namespace Database\Factories;

use App\Enums\UserRoleEnum;
use App\Models\Teacher;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class TeacherFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Teacher::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'first_name' => $this->faker->firstName(),
            'last_name' => $this->faker->lastName,
            'title' => $this->faker->optional(0.1)->title,
            'email' => $this->faker->unique()->freeEmail,
            'email_verified_at' => now(),
            'password' => 'yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
            'role' => UserRoleEnum::TEACHER,
        ];
    }
}
<?php

namespace App\Models;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Parental\HasParent;

class Teacher extends User
{
    use HasFactory, HasParent;

}
0 likes
26 replies
Nakov's avatar

It just seems like you are using an old version of faker.

This is what I have in my composer.json the require-dev list:

"fakerphp/faker": "^1.9.1",

it seems like it works.

jrdavidson's avatar

I have the same.

"fzaninotto/faker": "^1.9.1",
jrdavidson's avatar

Well what I did was composer remove fzaninotto/faker and then composer install fakerphp/faker and then went ahead and ran composer update and reran my test and still got the same result as before.

Nakov's avatar

try composer dump-autoload just in case it still uses wrong version :)

btw, you should've installed it using the dev flag, you don't need faker in production

composer install fakerphp/faker --dev
jrdavidson's avatar

Yeah that was a good idea but unfortunately nothing changed.

Nakov's avatar

and running:

composer show fakerphp/faker

what is the result?

This is what I have as a installed version:

versions : * v1.12.0

both the firstName and firstName() can be used.

jrdavidson's avatar

Here is a copy of the full stack trace.

InvalidArgumentException: Unknown formatter "firstName"

vendor/fakerphp/faker/src/Faker/Generator.php:248
vendor/fakerphp/faker/src/Faker/Generator.php:228
vendor/fakerphp/faker/src/Faker/Generator.php:285
database/factories/TeacherFactory.php:27
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:382
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:361
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:345
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:157
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:350
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:318
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:230
tests/Unit/Mail/WelcomeTeacherTest.php:17
jrdavidson's avatar
composer show fakerphp/faker
name     : fakerphp/faker
descrip. : Faker is a PHP library that generates fake data for you.
keywords : data, faker, fixtures
versions : * v1.13.0
type     : library
license  : MIT License (MIT) (OSI approved) https://spdx.org/licenses/MIT.html#licenseText
homepage : 
source   : [git] https://github.com/FakerPHP/Faker.git ab3f5364d01f2c2c16113442fb987d26e4004913
dist     : [zip] https://api.github.com/repos/FakerPHP/Faker/zipball/ab3f5364d01f2c2c16113442fb987d26e4004913 ab3f5364d01f2c2c16113442fb987d26e4004913
path     : /Users/jeffreydavidson/Projects/Apps/teach-me/vendor/fakerphp/faker
names    : fakerphp/faker

support
issues : https://github.com/FakerPHP/Faker/issues
source : https://github.com/FakerPHP/Faker/tree/v1.13.0

autoload
psr-4
Faker\ => src/Faker/

requires
php ^7.1 || ^8.0

requires (dev)
bamarni/composer-bin-plugin ^1.4.1
ext-intl *
phpunit/phpunit ^7.5.20 || ^8.5.8 || ^9.4.2

conflicts
fzaninotto/faker *
1 like
Nakov's avatar

What is interesting is that when I do this:

$ff = new \Faker\Generator();
dd($ff->firstName);

I get the same error as you do. But when I am using $this->faker->firstName all works good.

So the only thing left is your laravel version, in case the use Illuminate\Database\Eloquent\Factories\Factory; parent class still uses the old generator.

Running php artisan --version is 8.16.1 for me.

Nakov's avatar

Clear your cache is the last thing I can think of, php artisan config:clear and sorry but I have no other idea. Unless you use a docker container or homestead which might require even a restart. Other than that, I don't have a clue :)

Nakov's avatar

I don't see it in my vendor folder. So maybe remove the vendor folder and run composer install again. Don't know what to say.

Nakov's avatar

You can, if you have tests, because the .lock keeps previous versions.

jrdavidson's avatar

Yeah I did that and even based off my composer.json file it still has the ramsey/collection which has a dependency of "fzaninotto/faker": "^1.5",

            "name": "ramsey/collection",
            "version": "1.1.1",
            "source": {
                "type": "git",
                "url": "https://github.com/ramsey/collection.git",
                "reference": "24d93aefb2cd786b7edd9f45b554aea20b28b9b1"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/ramsey/collection/zipball/24d93aefb2cd786b7edd9f45b554aea20b28b9b1",
                "reference": "24d93aefb2cd786b7edd9f45b554aea20b28b9b1",
                "shasum": ""
            },
            "require": {
                "php": "^7.2 || ^8"
            },
            "require-dev": {
                "captainhook/captainhook": "^5.3",
                "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
                "ergebnis/composer-normalize": "^2.6",
                "fzaninotto/faker": "^1.5",
                "hamcrest/hamcrest-php": "^2",
                "jangregor/phpstan-prophecy": "^0.6",
                "mockery/mockery": "^1.3",
                "phpstan/extension-installer": "^1",
                "phpstan/phpstan": "^0.12.32",
                "phpstan/phpstan-mockery": "^0.12.5",
                "phpstan/phpstan-phpunit": "^0.12.11",
                "phpunit/phpunit": "^8.5",
                "psy/psysh": "^0.10.4",
                "slevomat/coding-standard": "^6.3",
                "squizlabs/php_codesniffer": "^3.5",
                "vimeo/psalm": "^3.12.2"
            },

But ramsey/collection has moved to a new version which uses the phpfaker library. So I don't know which of my dependencies uses ramsey/collection.

Nakov's avatar

in my opinion that should not cause any issue on your project nor tests. I am really out of ideas, tried everything on my end, all works as it should. Told you the only way I can reproduce your error, but other than forcing it, all works as expected.

JasonYoung's avatar
Level 3

Make sure that you're using Tests\TestCase in your unit test and not PHPUnit\Framework\TestCase if you want to use factories. This is a recent change in Laravel to increase performance in Unit tests.

10 likes
kevinruffe's avatar

Just a small correction. For me it was Tests\TestCase.

2 likes
benfurfie's avatar

Just in case anyone else comes across this thread, and they're using PestPHP. Check if the test you're getting the error on is a unit test. If it is, there's a chance you won't have told Pest to use Tests\TestCase, and it'll be falling back to PHPUnit\Framework\TestCase, as JasonYoung said.

The simple fix is to go into Pest.php in the tests folder, and duplicate the line:

uses(Tests\TestCase::class)->in('Feature');

And change Feature to Unit.

Run pest again, and that error should be gone. Whether or not you have another error is up to you!

2 likes
evanlalo's avatar

I was banging my head against a wall with the same problem. In my case, I had a setUp function in my test and I forgot to include parent::setUp() before using my factory.

    public function setUp(): void
    {
        parent::setUp();
        $this->model= Model::factory()->create();
    }
1 like
grantholle's avatar

I just ran into this situation. I was using the correct uses(), but it still wasn't working.

The issue was I was using datasets and yield. I guess inside of the yield block, things aren't fully bootrapped for the test and it will return an invalid formatter. I had to change to an array of functions.

nomitoor's avatar

Make sure if you are using setup method on your test class, use

parent::setUp();

on top of setup method. Thanks

Please or to participate in this conversation.