I’m stuck… I’m trying to get a set of factories working in Laravel 6, an continuously get the following error:
InvalidArgumentException with message 'Unable to locate factory with name [default] [User].'
There’s several other posts about this on here and around the web, and I’ve tried all of those solutions. My code for the factories is as follows:
<?php
/** @var IlluminateDatabaseEloquentFactory $factory */
use AppThread;
use FakerGenerator as Faker;
$factory->define(Thread::class, function (Faker $faker) {
return [
'user_id' => function () {
return factory('AppUser')->create()->id;
},
'title' => $faker->sentence,
'body' => $faker->paragraph
];
});
I’m using php artisan tinker to try and generate DB entries so I can work on the rest of my models etc. The command I’m using is as follows:
factory('Thread',50)->create()
I’ve tried specifying the name space in the factory call, I’ve tried putting it in double quotes, I’ve tried adding the ‘::class’ at the end. The controllers are there, the models are there, the migrations are working as expected, but every time I run the factory in tinker, it won’t give it to me. Most of the documentation that I’ve found is referencing older versions of Laravel as well, so I’m not sure if they are providing me with correct syntax.
It’s worth noting, the stock provided UserFactory doesn’t work either. This is a fresh download of Laravel 6 that I did today to go through a course over at Laracasts. The course is older, focused on Laravel 5.4, but I can figure out the conversion… I just can’t get this to work to save my life and I can’t figure out why.
I’ve got the whole project uploaded here if you want to view additional code: https://github.com/aarpie100/laracasts_forum
Help?
2
Answers
Try sending Thread::class instead of Thread as a string. If you are sending as string it should be ‘AppThread’ I think.
Factory function When you call
the class name you should write it like this (Thread :: class)