brandongrando's avatar

Unable to find Model file in function within Factory file

Hi everyone,

I'm getting the following error when trying to create article with the factory method as a part of the laravel from scratch course. I'm trying to create 10 random articles from tinker.

>>> Article::factory()->count(10)->create();
PHP Error:  Class 'Database/Factories/App/Models/User' not found in C:/freshproject/database/factories/ArticleFactory.php on line 25

I've triple and quadruple checked everything and I can't seem to find why the file isn't being drawn from the root directory - seems to be the only thing wrong. I've had to change the original code form the course as I found that the functionality is different in laravel 8. I'm assuming i'm missing something in translation. This is my factory code:

<?php

namespace Database\Factories;

use App\Models\Article;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'user_id' => function () {
                return \App\Models\User::factory();
            },
            'title' => $faker->sentence,
            'excerpt' => $faker->paragraph,
            'body' => $faker->paragraphs(5)
        ];
    }
}

Any help would be appreciated!

0 likes
10 replies
brandongrando's avatar

Also tried

use App\Models\User;

and

        return [
            'user_id' => function () {
                return User::factory();
            },
            'title' => $faker->sentence,
            'excerpt' => $faker->paragraph,
            'body' => $faker->paragraphs(5)
        ];

But wasn't able to make that work either.

Sinnbeck's avatar

That should work. Only error I see is you are missing $this for faker

            'title' => $this->faker->sentence,
            'excerpt' => $this->faker->paragraph,
            'body' => $this->faker->paragraphs(5)
Sinnbeck's avatar

Give this a shot

'user_id' => \App\Models\User::factory(),
brandongrando's avatar

Hi Sinnbeck, thanks for jumping in again!

>>> Article::factory()->count(10)->create();
PHP Error:  Class 'Database/Factories/App/Models/User' not found in C:/freshproject/database/factories/ArticleFactory.php on line 27
>>> Article::factory()->count(10)->create();
PHP Error:  Class 'Database/Factories/App/Models/User' not found in C:/freshproject/database/factories/ArticleFactory.php on line 27
>>> Article::factory()->count(10)->create();
PHP Error:  Class 'Database/Factories/App/Models/User' not found in C:/freshproject/database/factories/ArticleFactory.php on line 27

The first two are the errors when I make those changes, and third is when I try the second method with a '' before 'App'. The directory seems to be added onto the end of "Database\Factories' no matter what I try.

Sinnbeck's avatar

Sorry missed a \. Added to my answer :)

brandongrando's avatar

That was the third thing I tried. Still seems to be trying to find the file in the factories folder. Not too sure what to try!

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I cannot recreate it sadly.

Perhaps try dumping your autoload

composer dump-autoload

Also I suggest you use the use App\Models\User; method in your file :)

brandongrando's avatar

After a bit of playing around the dump-autoload seemed to do the trick! Will have to do some research on what I actually did there :)

Thanks again for your help!

alessandrofilira's avatar

I have the same error. My situation is an updated of laravel code with different base Namespace. Debugging code i found that in vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php row 689 there is:

public static function resolveFactoryName(string $modelName) {

    $resolver = static::$factoryNameResolver ?: function (string $modelName) {
        $modelName = Str::startsWith($modelName, 'App\Models\')
            ? Str::after($modelName, 'App\Models\')
            : Str::after($modelName, 'App\');
        return static::$namespace.$modelName.'Factory';
    };

    return $resolver($modelName);
}

As you can see the code expects that namespace is App and not another string.

I solved the problem creating a custom command as explain here: https://gist.github.com/isluewell/b824c0aef32f5007170fcd0d8498b657 and then I regenerated autoload with composer dump-autoload

I hope this can be useful because I "lost" some hours to find it.

jeff777's avatar

Worked for me after running php artisan migrate:refresh

Please or to participate in this conversation.