Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel faker factory not found


Laravel faker factory not found


The error "Class 'Faker\Factory' not found" occurs when the Faker library is not properly loaded in your Laravel application. This can happen due to a few reasons:

1. The Faker package is listed in the `require-dev` section of `composer.json`, but you are running `composer install --no-dev` during deployment, which skips installing development dependencies[1]. To fix this, either move `"fzaninotto/faker": "~1.5"` to the `require` section or remove the `--no-dev` option when deploying.

2. If you recently upgraded to Laravel 8 or 9 and are using the legacy factory pattern, you need to explicitly add `"fzaninotto/faker": "^1.9.1"` to your `composer.json` file's `require` section[2][3].

3. If you are using the `nwidart/laravel-modules` package and run `composer install --no-dev` when `APP_ENV=local`, you will get this error. This is a known issue with the package[5].

4. In some cases, using the `production` environment can cause this error, as the `Faker\Factory` class is not available. To fix it, change the `.env` file to use `APP_ENV=local` instead of `production`, then run `composer install` to reinstall all packages, including Faker[4].

The general solution is to ensure that the Faker library is properly installed by either:

- Moving `"fzaninotto/faker": "~1.5"` from `require-dev` to `require` in `composer.json`
- Explicitly adding `"fzaninotto/faker": "^1.9.1"` to the `require` section for Laravel 8/9 with legacy factories
- Running `composer install` without the `--no-dev` option during deployment
- Setting `APP_ENV=local` in the `.env` file and running `composer install`

After making the necessary changes, run `composer install` or `composer update` to install or update the Faker package, and the error should be resolved.

Citations:
[1] https://stackoverflow.com/questions/32801183/deployment-on-laravel-forge-throwing-faker-not-found-exception
[2] https://laracasts.com/discuss/channels/laravel/class-fakerfactory-not-found-in-laravel-9
[3] https://laracasts.com/discuss/channels/laravel/error-class-fakerfactory-not-found-after-upgrade-to-laravel-8-and-use-factory-legacy
[4] https://akaunting.com/forum/discussion/development/error-class-fakerfactory-not-found
[5] https://github.com/nWidart/laravel-modules/issues/951