Call to undefined function Database\Seeders\factory()

50 Views Asked by At

I have that error, since i create a UserFactory and in my UserTableSeeder add this line factory(App\User::class,10)->create(); here a picture

In my UserTableSeeder i replace factory(App\User::class,10)->create(); by this \App\Models\User::factory()->count(10)->create(); In my UserFactory i replace 'username' => $faker->name, by this'username' => $this->faker->text(10) no results Thank you for your help

enter image description here

1

There are 1 best solutions below

6
edpmath On BEST ANSWER

In your model User.php

namespace App\Models; // App instead app
// ...
use Illuminate\Database\Eloquent\Factories\HasFactory;
// ...

class User extends Authenticatable
{
    use HasFactory;
//...
}

In your seeder:

\App\Models\User::factory(10)->create([
    // Some optional static fields
    'role' => 'user',
    ]);

Additionally, you are confused about using app instead App for your namespace declaration because the app folder is in lowercase, but you need to see composer.json file and you will find this lines:

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },

The app folder is parsed as App namespace, so you need allways use namespace App\... in in declarations, and \App or App in the uses.