php Laravel 5.2:无法找到名称为 [默认] 的工厂
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36421287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2: Unable to locate factory with name [default]
提问by paranoid
I want to seed database when I use this
我想在使用它时为数据库做种
public function run()
{
$users = factory(app\User::class, 3)->create();
}
Add three user in database but when I use this
在数据库中添加三个用户但是当我使用它时
public function run()
{
$Comment= factory(app\Comment::class, 3)->create();
}
Show me error
显示错误
[InvalidArgumentException]
Unable to locate factory with name [default] [app\Comment].
[InvalidArgumentException]
无法找到名称为 [默认] [app\Comment] 的工厂。
回答by Darmen Amanbayev
If nothing helps with PHPUnit.
如果没有任何帮助PHPUnit。
For those of readers who stuck with the same issue in tests, I found out that I forgot to add parent::setUp()
in setUp
method.
对于那些谁坚持在测试同一个问题的读者,我发现我忘了补充parent::setUp()
的setUp
方法。
回答by Ayeni TonyOpe
Some times it could be due to importing the wrong TestCase
有时可能是由于导入了错误的TestCase
use PHPUnit\Framework\TestCase;
[WRONG: and throws this error]
use PHPUnit\Framework\TestCase;
[错误:并引发此错误]
use Tests\TestCase;
[CORRECT]
use Tests\TestCase;
[正确的]
回答by Mohan
By default the laravel installation comes with this code in the database/factories/ModelFactory.php
File.
默认情况下,laravel 安装在database/factories/ModelFactory.php
文件中带有此代码。
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
];
});
So you need to define a factory Model before you use it to seed database. This just uses an instance of Faker Librarywhich is used to generate fake Data for seeding the database to perform testing.
因此,您需要先定义一个工厂模型,然后再使用它来播种数据库。这只是使用Faker 库的一个实例,该实例用于生成假数据,以便为数据库播种以执行测试。
Make sure You have added a similar Modal Factory for the Comments Model.
确保您已经为评论模型添加了一个类似的模态工厂。
So your Comments Model Factory will be something like this :
所以你的评论模型工厂将是这样的:
$factory->define(App\Comment::class, function (Faker\Generator $faker) {
return [
'comment' => $faker->sentence,
// Any other Fields in your Comments Model
];
});
回答by Agu Dondo
This can also happen when you are running the command factory()->create()
from php artisan tinker
. Make sure you save the file database/factories/ModelFactory.php
before opening tinker
当您factory()->create()
从php artisan tinker
. 确保database/factories/ModelFactory.php
在打开 tinker 之前保存文件
回答by Cherma Ramalho
Step - ? Make sure CommentFactory is using Comment instead of Model.
use App\Comment ...
$factory->define(Comment::class, function (Faker $faker){
Step - Verify that the names are correct in CommentsTableSeeder.
use App\Comment ...
public function run() { factory(Comment::class, 3)->create(); }
步 - ?确保 CommentFactory 使用的是 Comment 而不是 Model。
使用 App\Comment ...
$factory->define(Comment::class, function (Faker $faker){
步骤 - 验证 CommentsTableSeeder 中的名称是否正确。
使用 App\Comment ...
public function run() { factory(Comment::class, 3)->create(); }
Good luck!
祝你好运!
回答by vimuth
I'm using laravel 5.5and for that doing this is bit different. You have to create CommentFactory.phpinside \database\factoriesdirectory and add this inside,
我正在使用laravel 5.5,为此这样做有点不同。您必须 在\database\factories目录中创建CommentFactory.php并将其添加到其中,
$factory->define(App\Comment::class, function (Faker\Generator $faker) {
return [
'comment' => $faker->sentence,
// Any other Fields in your Comments Model
];
});
And add this,
并添加这个,
$Comment= factory(\App\Comment::class, 3)->create();
instead of
代替
$Comment= factory(app\Comment::class, 3)->create();
I just wanted to add this since I'm facing the same issue for later version and this thread helped me a lot to fix it.
我只是想添加这个,因为我在以后的版本中遇到了同样的问题,这个线程帮助我解决了很多问题。
回答by grzeniufication
In my case the error was that I've imported the wrong test base class. Instead of extending Laravel's Tests\TestCase I imported the TestCase class from PHPUnit. Silly, but it took me quite some time to figure this out.
就我而言,错误是我导入了错误的测试基类。我没有扩展 Laravel 的 Tests\TestCase,而是从 PHPUnit 导入了 TestCase 类。愚蠢,但我花了很长时间才弄清楚这一点。
回答by Yarco
I'm using Laravel Framework 5.7.19
. And in my case, my factory file is generated from command line make:factory
. The default model in the file is:
我正在使用Laravel Framework 5.7.19
. 就我而言,我的工厂文件是从命令行生成的make:factory
。文件中的默认模型是:
$factory->define(Model::class, ...
You should change the name Model
to the thing you exactly want to use, in my case it is \App\InterviewQuestion
, so it becomes:
您应该将名称更改为Model
您确切想要使用的名称,在我的情况下是\App\InterviewQuestion
,因此它变为:
$factory->define(\App\InterviewQuestion::class, ...
回答by Abhilash Asokan
This could be a cache problem. You can resolve it executing the commands following commands.
这可能是缓存问题。您可以通过执行以下命令来解决它。
php artisan clear-compiled
composer dump-autoload
php artisan optimize
回答by user2682025
None of the answers above did work for me. The best way to fix this error entirely is by registering your factories in TestCase class like that:
上面的答案都不适合我。完全修复此错误的最佳方法是在 TestCase 类中注册您的工厂,如下所示:
<?php
namespace Vendo\Package\Tests;
use Vendor\Package\InboxServiceProvider;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
class TestCase extends \Orchestra\Testbench\TestCase
{
public function setUp(): void
{
parent::setUp();
// additional setup
$this->app->make(EloquentFactory::class)->load(__DIR__ . '/../database/factories');
}