类'UserTableSeeder'不存在-Laravel 5.0 [php artisan db:seed]
迁移数据库后,我正在尝试基本的php artisan db:seed,但它始终在cmd中返回标题错误-[ReflectionException]类'UserTableSeeder'不存在
I'm trying a basic php artisan db:seed after migrating my database but it keeps returning the title error in cmd -[ReflectionException] Class 'UserTableSeeder' does not exist
我尝试过的事情
- 更改"UserTableSeeder.php"文件名称空间数据库\种子"的名称空间;和使用Database \ seeds \ UserTableSeeder;"在"DatabaseSeeder.php"文件中
下面是迁移
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
下面是UserTableSeeder.php
Below is the UserTableSeeder.php
<?php
use App\User;
use Illuminate\Database\Seeder;
class UserTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
User::create(['email' => 'foo@bar.com']);
}
}
下面是DatabaseSeeder.php
Below is the 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('UserTableSeeder');
}
}
在数据库/文件夹中创建文件后运行composer dumpautoload
.
Run composer dumpautoload
after creating files in the database/ folder.
为什么?
检查composer.json
自动加载部分,您会看到database/
文件夹已由"classmap"加载(
Check the composer.json
autoload section and you'll see the database/
folder is loaded by "classmap" (source):
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
Composer 文档将类映射描述为:
The Composer docs describe classmap as:
在安装/更新过程中,类映射引用全部合并为 单个键=>值数组,可以在生成的文件中找到 供应商/composer/autoload_classmap.php. 此地图是通过扫描建立的 给定目录/文件中所有.php和.inc文件中的类.
The classmap references are all combined, during install/update, into a single key => value array which may be found in the generated file vendor/composer/autoload_classmap.php. This map is built by scanning for classes in all .php and .inc files in the given directories/files.
您可以使用类映射生成支持来定义自动加载 不遵循PSR-0/4的所有库.要配置这个,你 指定所有目录或文件以搜索类.
You can use the classmap generation support to define autoloading for all libraries that do not follow PSR-0/4. To configure this you specify all directories or files to search for classes.
已添加重点.每次将文件添加到database/
时,都需要运行composer dumpautoload
命令生成一个新的类映射,否则它将不会自动加载.
Emphasis added. You need to run the composer dumpautoload
command to generate a new classmap every time you add a file to database/
, otherwise it will not be autoloaded.
相反,app/
文件夹使用 PSR-4 标准,用于将完全限定的类名转换为文件系统路径.这就是为什么在其中添加文件后无需dumpautoload
的原因.
The app/
folder, by contrast, uses the PSR-4 standard for converting a fully qualified class name to a filesystem path. This is why you don't need to dumpautoload
after adding files there.