Laravel: Call to undefined method App\Models\Category::factory()
Call to undefined method App\Models\Category::factory()Short answer
The static factory() method is added to a model by the HasFactory trait. If the model does not use that trait, PHP has no such method and throws this error. Add the trait with its full namespace. If the factory class then cannot be found, name it by convention or point the model at it.
Fix it fast
1Find out which cause you have
Open the model and look for the trait. If there is no use HasFactory line inside the class, that is the cause.
grep -n "HasFactory" app/Models/Category.php2Match what you see, then jump to its fix
What does the error look like?
Call to undefined method App\Models\Category::factory()Class "Database\Factories\CategoryFactory" not found
Cause 1 of 2
The model does not use the HasFactory trait
1Check
The factory() method is provided by Illuminate\Database\Eloquent\Factories\HasFactory. Models made by current make:model include it; older or hand-written models often do not.
sed -n '1,25p' app/Models/Category.php2Fix
Import the trait and use it inside the class.
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
}3Verify
Create a record through the factory in tinker.
php artisan tinker
>>> App\Models\Category::factory()->make();Confirmed by laravel.com
Cause 2 of 2
The model has the trait but the factory is not found by convention
1Check
HasFactory looks for a class in the Database\Factories namespace with the model's name plus Factory. Check that the file exists and that its namespace and class name match.
ls database/factories/
grep -n "namespace\|class " database/factories/CategoryFactory.php2Fix
Generate the factory with Artisan, or keep your own layout and tell the model where the factory is, with the UseFactory attribute or a newFactory method.
php artisan make:factory CategoryFactory
// custom location
use Database\Factories\Shop\CategoryFactory;
use Illuminate\Database\Eloquent\Attributes\UseFactory;
#[UseFactory(CategoryFactory::class)]
class Category extends Model { use HasFactory; }3Verify
Run the same tinker call again.
php artisan tinker
>>> App\Models\Category::factory()->count(3)->make();Confirmed by laravel.com
How do I stop it coming back?
- Create models with php artisan make:model --factory so the trait and the factory arrive together.
- Keep factory names as ModelNameFactory in database/factories, and only use UseFactory when you must break that rule.
- Run the seeders and tests in CI so a missing trait fails the build, not a deploy.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix