迁移我的数据库后,我正在尝试一个基本的 php artisan db:seed,但它一直在 cmd 中返回标题错误 -[ReflectionException] Class 'UserTableSeeder' does not exist
我尝试过的事情
- 更改'UserTableSeeder.php'文件'namespace Database\seeds;'的命名空间和'使用数据库\种子\用户表种子;'在“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
<?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
<?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
。
为什么?
检查 composer.json
自动加载部分,您会看到 database/
文件夹由“classmap”(source)加载:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
Composer docs将 classmap 描述为:
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.
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
命令生成新的classmap,否则不会自动加载。
相比之下,app/
文件夹使用 PSR-4将完全限定的类名转换为文件系统路径的标准。这就是为什么在添加文件后不需要dumpautoload
。
关于php - 类 'UserTableSeeder' 不存在 - Laravel 5.0 [php artisan db :seed],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36206742/