Laravel 5.2: Unable to locate factory with name [default]

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Understanding and Resolving Laravel 5.2 Database Seeding Issues Body: Introduction: As a senior developer in the realm of web application development, you might have come across situations when your code doesn't work as expected or produces unexpected results. One such instance could occur while using factories in database seeding with Laravel 5.2. This blog post will walk you through the possible causes for error messages like "[InvalidArgumentException] Unable to locate factory with name [default] [app\Comment]" and provide solutions that'll help you prevent or fix them. A. Factory Names and Database Seeding Laravel makes use of Factories (Eloquent models) during database seeding, which helps simplify the creation of records in the database. To create a factory, you need to define it using the `\Factory::define()` method. This will automatically create a corresponding file in your app's `database/factories` directory.
use Faker\Generator as Faker;

/** @var \Illuminate\Database\Eloquent\Factory */
$factory->define(\App\Comment::class, function (Faker $faker) {
    return [
        'body' => $faker->paragraph(),
        'user_id' => factory(app\User::class)->create()->id,
    ];
});
This example creates a factory for the `App\Comment` model. When you call the factory method during seeding, the corresponding file will be searched and used to generate the required records in your database. B. Debugging Factory Issues If you encounter an error like "[InvalidArgumentException] Unable to locate factory with name [default] [app\Comment]", it often means that Laravel is unable to find or access the specified factory. Usually, this can happen due to one of the following reasons: 1. Incorrect Factory Name Referencing: Check if you have provided the correct factory name for your model class in the seeding file or code. For instance, the above error implies that Laravel is trying to locate a factory named "default", but it should be looking for the newly created `App\Comment` factory. 2. Incorrect Factory File Path: Sometimes, the factory file doesn't exist at the designated location (app/database/factories) or has an inappropriate name to match your model class. Verify the factory's path and file name by checking inside your application's `database` directory. Make sure the factory you have created aligns with the corresponding model class. 3. Missing Namespace or Class Path: Ensure that the namespace for your class is correct and accessible within your Laravel project. If necessary, update your factory definition to specify the full path of the model class, using the `app\` prefix if needed. C. Preventing and Solving Factory Errors To ensure a seamless experience with factories in database seeding: 1. Create factories for each table or Eloquent models. This will help avoid confusion between different kinds of factory names. 2. Ensure that your factory definitions match the corresponding model class's namespace and path. 3. Always test your code before you commit it to ensure that there are no errors during seeding and factory usage. 4. Follow best practices for Laravel 5.2, including updating your project with the latest version and following its guidelines. This will prevent common issues related to incompatibility or deprecation of certain features. Conclusion: Laravel factories are essential tools while working with database seeding. However, they must be created, defined, and used correctly for them to work as intended. By understanding the reasons for factory errors and following best practices, you can easily avoid and resolve such issues in your Laravel 5.2 project. For more resources on Laravel development, visit our website https://laravelcompany.com.