jmason81's avatar

Class Faker\Factory not found in Laravel 9

I'm getting this error when trying to run a factory that is using faker:

PHP Error: Class "Faker\Factory" not found in ../vendor/laravel/framework/src/Illuminate/Database/DatabaseServiceProvider.php on line 93

I don't know if I have to install faker separately? I thought it was bundled in Laravel and can't seem to find anything that would indicate otherwise.

My factory class:

namespace Database\Factories;

use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;


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

/**
 * Define the model's default state.
 *
 * @return array
 */
public function definition()
{
    return [
        'first_name' => $this->faker->firstName,
        'last_name' => $this->faker->lastName,
        'phone' => $this->faker->unique()->numerify('###-###-####'),
        'email' => $this->faker->unique()->safeEmail,
        'role_id' => rand(1,4),
        'remember_token' => Str::random(10),
    ];
}

I'm not requiring anything additional in my composer.json file. My environment is set to local.

0 likes
3 replies
jmason81's avatar

In Tinker I'm running User::factory()->count(10)->create()

Niush's avatar

Most probably faker is not installed. Either you ran composer install with --no-dev flag. Or it failed installing.

Try installing again with composer install and updating with composer update.

Make sure the composer.json has faker in it like such:

"require-dev": {
    "fakerphp/faker": "^1.9.1",
    // ...
}
12 likes

Please or to participate in this conversation.