Laravel 5.2: Unable to locate factory with name [default]
If you are migrating from older Laravel versions or working within a legacy codebase, encountering the InvalidArgumentException: Unable to locate factory with name [default] is a classic symptom of outdated dependency management practices. This specific error usually manifests when attempting to create model factories using the global facade syntaxâsuch as factory(app\User::class, 3)->create()âin an environment where the underlying Factory package has been updated or replaced.
In Laravel 5.2 and earlier, the community heavily relied on the mews/factory package to generate realistic test data. The magic that allowed you to write factory(User::class) worked because the facade was explicitly mapped to the Mews namespace. However, as the ecosystem evolved, specifically with the introduction of Laravel 5.3 and the subsequent release of Laravel 5.4, the framework natively adopted the spatie/laravel-factories package (and later integrated factory support directly into the core).
When you run your seeder using the old facade syntax on a project that has already migrated to the native implementation or installed the Spatie package, the autoloader cannot resolve the request. The error message [InvalidArgumentException] Unable to locate factory with name [default] indicates that the framework is looking for a default factory class but failing to find it because the mapping between the facade and the actual factory definitions has broken.
The root cause is almost always the mismatch between the syntax used in your seeder and the package currently installed in your composer.json. If you are using Laravel 5.2, you likely have mews/factory installed. If you have manually updated dependencies or are running on a newer version without realizing it, you might have spatie/laravel-factories (or native support) active. The facade call factory(Model::class) simply does not exist in the newer implementations in the same way.
To resolve this, you must align your code with the available package. If you are committed to staying on Laravel 5.2, ensure that mews/factory is installed and that your config/app.php file correctly maps the facades. Look for the aliases section and verify that 'Factory' => 'Mews\Fake\Factory' (or similar) is present. Without this alias, the global facade remains undefined, triggering the exception.
If you are planning to upgrade out of Laravel 5.2, simply removing the old dependency and switching to the native syntax is the recommended path. Instead of relying on a facade, you should instantiate the factory directly:
use App\User;
use Spatie\Activitylog\Models\Activity; // Example of importing specific factory
public function run()
{
$users = factory(User::class, 3)->create();
// Or using the native approach in Laravel 5.4+
// $users = User::factory()->count(3)->create();
}
The trade-off here is code portability. The Mews facade was convenient but tied you to a third-party package that eventually saw little maintenance compared to the core team's direction. By switching to the native syntax or Spatie's implementation, you gain compatibility with future Laravel versions without needing to constantly update vendor dependencies.
Another pitfall to watch for is the namespace of your models. In the error snippet provided, app\Comment::class is passed to the factory. If you are using the old Mews package, it expects a specific structure where factories were defined globally. If your AppServiceProvider or config/app.php does not explicitly tell Laravel which class corresponds to the 'default' factory name, this ambiguity triggers the exception.
Ultimately, the fix requires auditing your composer.json. If you see mews/factory, keep it and ensure the alias exists. If you do not want that package, remove it, update your code to use the modern Model::factory() syntax or the Spatie package, and run composer dump-autoload to refresh the class map. This ensures that when your seeder runs, the framework knows exactly which factory class to instantiate for your models.