VertexBuffer's avatar

Unable to locate factory with name [default] [App\User].

Trying to create a model via factory like so on a fresh Laravel project; factory(\App\User::class)->create();

For whatever reason it keeps spitting out the error; Unable to locate factory with name [default] [App\User].

Even though the file definitely exists. Not really sure what is causing this works in plenty of other projects.

0 likes
10 replies
Tray2's avatar

What does your UserFactory.phplook like?

mushood's avatar

Can you show the code for the factory?

use App\Models\User;


.
.
.


$factory->define(User::class, function (Faker $faker) {
VertexBuffer's avatar

@tray2 @mushood

I've removed the actual attributes to just simplify.

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(User::class, function (Faker $faker)
{
    return [
    ...
    ];
});
mushood's avatar

Could you try this?

Where you are calling the factory

use App\User;

.
.
.
.
factory(User::class)->create();
VertexBuffer's avatar

@mushood Inside my UserTest file.

    use App\User;

    public function my_user_test()
    {
        $user = factory(User::class)->create();
    }

Same error with code above. InvalidArgumentException : Unable to locate factory with name [default] [App\User].

mushood's avatar

Very weird.

Don't see anything wrong

Maybe try clearing cache

php artisan cache:clear

composer dump-autoload
Tray2's avatar

Is the UserFactory.php placed in the correct directory?

database/factories/UserFactory.php

VertexBuffer's avatar
VertexBuffer
OP
Best Answer
Level 7

Ok I solved the issue. For whatever reason, the UserTest had the use statement of; use PHPUnit\Framework\TestCase; Where as it should've been; use Tests\TestCase;

I have no clue why this was like this and I do not recall changing it at all. Not sure if Laravel comes with this by default?

Please or to participate in this conversation.