Call to undefined method App\Models\Category::factory() laravel
When running php artisan db:seed, you encounter a BadMethodCallException stating that App\Models\Category::factory() is undefined. This error stems from Laravel's eager loading mechanism expecting specific factory definitions, but it doesn't automatically register them for every model you touch in your seeders. The stack trace points directly to the execution of \App\Models\Category::factory(), indicating that the framework cannot find a corresponding CategoryFactory class registered in its internal list.
In Laravel 8 and earlier, factories were often auto-discovered if they were placed in the database/factories directory and named after their model. However, starting with Laravel 9, this behavior changed significantly. The framework no longer automatically registers factories based on file location. Instead, you must explicitly define them in the configuration file to ensure they are accessible globally within your application.
The root cause of your issue lies in the mismatch between your DatabaseSeeder usage and your factory registration strategy. While you have created a valid CategoryFactory class in the Database\Factories namespace, Laravel does not know about it unless you register it. Without this registration, calling Category::factory() results in the "undefined method" exception because the magic method resolution fails to find a matching implementation.
To resolve this, you need to update your configuration file located at config/factories.php. Open this file and add the following entry:
CategoryFactory::class => [
Database\Factories\CategoryFactory::class,
],
This line explicitly tells Laravel that when it encounters a call to Category::factory(), it should resolve it to the Database\Factories\CategoryFactory class. Once this registration is in place, your seeder code will execute without throwing the exception, and you can proceed with seeding your categories.
Another critical detail in your migration file is the inclusion of the $table->integer('quantity') and $table->string('status') fields. Your Category model defines fillable attributes as only 'name' and 'description', but the migration creates columns for quantity, status, image, and seller_id. If you attempt to fill these extra columns without updating your model or using mass assignment protection exceptions, you might face further issues. Additionally, ensure that the Database\Factories\CategoryFactory::definition() method does not attempt to fill these new columns unless they are marked as fillable in the model or handled via $this->faker with appropriate fallbacks.
The migration also includes a foreign key constraint on seller_id referencing the users table. Before seeding categories, it is crucial that your users are seeded and present in the database. Your current seeder truncates all tables before creating new ones, which is fine for a fresh seed, but ensure the order of operations respects foreign key dependencies. For instance, if you were not truncating and instead inserting data, you would need to create users first, then categories, then products.
If you prefer not to use the configuration file approach, you can alternatively instantiate the factory directly in your seeder by fully qualifying the class name:
Database\Factories\CategoryFactory::new()->count($cantidadUsuarios)->create();
This bypasses the need for global registration but is less elegant and harder to maintain as your application grows. Using the configuration file is the standard practice for Laravel 9+ projects and ensures consistency across your codebase.
Finally, review your seeder logic regarding the Product seeding. You are attaching random categories to products based on a quantity derived from users. Ensure that the categories relationship in your Product model is correctly defined as a many-to-many or pivot table relation to match the category_product table structure. If the relationship definition does not match the migration schema, you may encounter runtime errors when trying to attach categories to products after successfully seeding them.
By registering the factory and verifying your model-migration alignment, you eliminate the undefined method error and establish a solid foundation for your database seeding workflow.
