ReflectionException - 类 DatabaseSeeder 不存在,Laravel Seeder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30408365/
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
ReflectionException - Class DatabaseSeeder does not exist , Laravel Seeder
提问by Iván Casanova
I have database seeder clases in diferent folder. When i write db:seed the console shows this error:
我在不同的文件夹中有数据库播种机类。当我写 db:seed 时,控制台显示此错误:
[ReflectionException] Class DatabaseSeeder does not exist , Laravel Seeder
One class is this:
一类是这样的:
namespace Database\Seeds;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use TiposCompromisosTableSeeder;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
$this->call('TiposCompromisosTableSeeder');
}
}
and my other class is
我的另一堂课是
namespace Database\Seeds;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class TiposCompromisosTableSeeder extends Seeder{
public function run(){
DB::table('tipos')->insert(array(
'nombre' => 'prioridad',
'tabla' => 'compromisos',
'str1' => 'baja',
'int1' => 1
));
}
}
I've tried to use
我试过用
composer dump-autoupload
but doesn't work.
但不起作用。
As you can see, I have both clases in the same namespace.
如您所见,我在同一个命名空间中有两个类。
Help please.
请帮忙。
回答by vladko
If you've recently upgraded your Laravel version, check your composer.json
如果您最近升级了 Laravel 版本,请检查您的 composer.json
Your "autoload" section should look something like the snippet below
您的“自动加载”部分应该类似于下面的代码段
NOTE: you may have to add the "database" entry under the "classmap"
注意:您可能需要在“classmap”下添加“database”条目
"autoload": {
"classmap": [
"app/Library",
"app/Models",
"database"
],
"psr-4": {
"App\": "app/"
},
"files": [
"app/Library/helpers.php"
]
},
You should then run composer dumpautoload
and try php artisan db:seed
然后你应该运行composer dumpautoload
并尝试php artisan db:seed
回答by Brian H.
Just put it all in the DatabaseSeeder.php file like this:
只需将其全部放入 DatabaseSeeder.php 文件中,如下所示:
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
$this->call('TiposCompromisosTableSeeder');
}
}
class TiposCompromisosTableSeeder extends Seeder{
public function run(){
DB::table('tipos')->insert(array(
'nombre' => 'prioridad',
'tabla' => 'compromisos',
'str1' => 'baja',
'int1' => 1
));
}
}
回答by alyosha
remove your namespace definition in two classes, and use "composer dump-autoload".
删除两个类中的命名空间定义,并使用“composer dump-autoload”。
Then it will works fine.
然后它会正常工作。
回答by Mohammad Selim Miah
Solved: by adding
解决:通过添加
namespace database\seeds;
命名空间数据库\种子;
and then running command:
然后运行命令:
composer dump-autoload --no-dev
作曲家转储自动加载 --no-dev