php Laravel 5:未找到 DB Seed 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30176194/
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: DB Seed class not found
提问by JasonGenX
I have this DatabaseSeeder.php:
我有这个 DatabaseSeeder.php:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call('MemberInvitationSeeder');
}
}
I have this file MemberInvitationSeeder.php, sibling to the DatabaseSeeder.php file
我有这个文件 MemberInvitationSeeder.php,DatabaseSeeder.php 文件的兄弟
<?php
use Illuminate\Database\Seeder;
use App\MemberInvitation;
class MemberInvitationSeeder extends Seeder {
public function run()
{
MemberInvitation::truncate();
MemberInvitation::create( [
'id' => 'BlahBlah' ,//com_create_guid(),
'partner_id' => 1,
'fisrt_name' => 'Thats',
'last_name' => 'Me',
'email' => '[email protected]',
'mobile_phone' => '444-342-4234',
'created_at' => new DateTime
] );
}
}
Now I call
现在我打电话
php artisan db:seed
and I get:
我得到:
[ReflectionException]
Class MemberInvitationSeeder does not exist
I tried everything I could find including "composer dump-autoload". to no avail. What am I doing wrong?
我尝试了我能找到的一切,包括“作曲家转储自动加载”。无济于事。我究竟做错了什么?
采纳答案by JasonGenX
I believe I know the reason now.
我相信我现在知道原因了。
The new class MemberInvitationSeeder wasn't in the autoloaded classes in the composer.json file.
新类 MemberInvitationSeeder 不在 composer.json 文件的自动加载类中。
It wasn't there because I added that class manually.
它不在那里,因为我手动添加了该类。
Now, going forward, if I add such classes again, what should I use in order for my class to automatically to the autoloader?
现在,展望未来,如果我再次添加这样的类,我应该使用什么来让我的类自动加载到自动加载器?
回答by Krzysztof Raciniewski
Step one- generate seed:
第一步- 生成种子:
php artisan make:seed MemberInvitationSeeder
Step two- In DatabaseSeeder.php add line:
第二步- 在 DatabaseSeeder.php 添加行:
$this->call(MemberInvitationSeeder::class);
Step three:
第三步:
composer dump-autoload
Step four:
第四步:
php artisan db:seed
This should work
这应该工作
If this isn't the clue, check file composer.json and make sure you have this code in "autoload" section:
如果这不是线索,请检查文件 composer.json 并确保您在“自动加载”部分中有此代码:
"classmap": [
"database"
],
回答by entoniperez
I solved this by adding the class to the seeder file, with the instruction use
:
我通过将类添加到种子文件中解决了这个问题,指令use
如下:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\YourClassName;
回答by iahmedbacha
If the above solutions doesn't work, try this one.
You may have changed the namespace (by default, it's "App").
What you need to do is to go to the composer.json
file and check this:
如果上述解决方案不起作用,请尝试此方法。您可能已经更改了命名空间(默认情况下,它是“App”)。您需要做的是转到composer.json
文件并检查:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\": "app/"
}
},
If the namespace is App like this example, this solution is not for you.
如果命名空间是 App 像这个例子,这个解决方案不适合你。
Otherwise, take the namespace you found and insert that line into your seeder class:
否则,使用您找到的命名空间并将该行插入到您的播种机类中:
use NameSpaceFound\User;