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

ahoi's avatar
Level 5

Class "App\User" not found

I moved the User-model from app/Models/User.php to app/Domain/User/Models/User.php - now if I try to use the user factory, e.g.

User::factory()->make();

I am getting this result:

Psy Shell v0.11.22 (PHP 8.2.12 — cli) by Justin Hileman
> User::factory()->make();
[!] Aliasing 'User' to 'App\Domain\User\Models\User' for this Tinker session.

   Error  Class "App\User" not found.

The user model class

This is the user class:

<?php

namespace App\Domain\User\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Domain\User\Traits\HasRole;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasRole, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];
}

Some config

This is auth.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Domain\User\Models\User::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expiry time is the number of minutes that each reset token will be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    | The throttle setting is the number of seconds a user must wait before
    | generating more password reset tokens. This prevents the user from
    | quickly generating a very large amount of password reset tokens.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_reset_tokens',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Password Confirmation Timeout
    |--------------------------------------------------------------------------
    |
    | Here you may define the amount of seconds before a password confirmation
    | times out and the user is prompted to re-enter their password via the
    | confirmation screen. By default, the timeout lasts for three hours.
    |
    */

    'password_timeout' => 10800,

];

##Clearing caches

I also did the following:

composer dump-autoload
pa config:clear
pa cache:clear
pa config:cache

This did not solve the issue.

Searching for App\User globally

I also searched project-wide in PHPStorm for App\User without any success.

What did I miss?

0 likes
9 replies
Nakov's avatar

And in your UserFactory do you import from the correct namespace?

1 like
ahoi's avatar
Level 5

@Nakov

I think so:

<?php

namespace Database\Factories\Domain\User\Models;

use App\Domain\User\Enums\Role;
use App\Domain\User\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
 * @extends Factory<User>
 */
class UserFactory extends Factory
{
    protected static ?string $password;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'name'              => fake()->name(),
            'email'             => fake()->unique()->safeEmail(),
            'email_verified_at' => now(),
            'password'          => static::$password ??= Hash::make('password'),
            'remember_token'    => Str::random(10),
        ];
    }

    /**
     * Indicate that the model's email address should be unverified.
     */
    public function unverified(): static
    {
        return $this->state(fn(array $attributes) => [
            'email_verified_at' => null,
        ]);
    }
ahoi's avatar
Level 5

Ah, now that I looked at it again, I mentioned that I do not have

    protected                $model = User::class;

set in the factory. Having that set, the error disappears.

But I still don't know why. It was not included in the default factory that comes with Laravel after installing.

Nakov's avatar

@ahoi just remember that after making changes.. like if you found an import and you fixed it, you will need to restart the tinker session each time. If you try in your routes to add dd(User::first()); will it return a user or it will show similar error?

Nakov's avatar

@ahoi are you sure? Delete that one and run php artisan make:factory UserFactory --model=User and see if it creates it. Otherwise probably you have an old stub file.

ahoi's avatar
Level 5

@Nakov Yes, I did. I even restarted valet, because I thought about some weird OPCache bug or something ;-)

As described, I had to set $model property in the factory class. So thank you for your hint to double-check the Factory class.

ahoi's avatar
Level 5

@Nakov

Yes, this is the result of the make:factory command:

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\User>
 */
class UserFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            //
        ];
    }
}
Nakov's avatar

@ahoi you can publish the stub files and edit them: https://laravel-news.com/customizing-stubs-in-laravel

so that you don't have to remember to add the $model each time. Although I think that as long as you use the --model flag it should insert the model.. but not sure what else is going on there. Update to the latest version I guess.

martinbean's avatar

@ahoi If you’re going to move your models to non-conventional locations, then you need to explicitly define the model class in all of your factories.

Please or to participate in this conversation.