2015-11-09 99 views
4

我正在使用laravel 5.1。我正在嘗試運行數據庫播種命令。Laravel的數據庫播種5.1

我的表名是用戶

我的遷移文件如下

2015_11_09_194832_create_users_table.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'); 
    } 
} 

DatabaseSeeder.php

<?php 

use App\User; 
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(UsersTableSeeder::class); 
     Model::reguard(); 
    } 
} 

UsersTableSeeder.php

<?php 

// DatabaseSeeder.php 
use App\User; 
use Illuminate\Database\Seeder; 
use Illuminate\Database\Eloquent\Model; 



class UsersTableSeeder extends Seeder 
{ 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */ 
    public function run() 
    { 
     Model::unguard(); 
     DB::table('users'); 
     $users = array(
       ['name' => 'Ryan Chenkie', 'email' => '[email protected]', 'password' => Hash::make('secret')], 
       ['name' => 'Chris Sevilleja', 'email' => '[email protected]', 'password' => Hash::make('secret')], 
       ['name' => 'Holly Lloyd', 'email' => '[email protected]', 'password' => Hash::make('secret')], 
       ['name' => 'Adnan Kukic', 'email' => '[email protected]', 'password' => Hash::make('secret')], 
     ); 

     // Loop through each user above and create the record for them in the database 
     foreach ($users as $user) 
     { 
      User::create($user); 
     } 
     Model::reguard(); 
    } 
} 

雖然我試圖運行播種命令PHP工匠DB:種子我收到以下錯誤。

[ReflectionException] 
    Class UsersTableSeeder does not exist 

任何人都可以在這方面幫助我?

+1

這兩個類都位於相同的全局名稱空間中,因此不需要「使用」。嘗試運行「composer dump-autoload」 - 它應該重新加載播種列表。 –

+1

創建新的播種機後,您應該在項目根目錄下運行'composer dumpautoload' – Alex

+1

由於上述用戶評論說您需要在項目根目錄下使用'composer dump-autoload'。您可以在終端中使用相應的命令 –

回答

2

當我在新項目中添加播種器或添加一些要測試的數據的現有項目時,我遇到過這個問題。

jedrzej.kurylo和Alex都在正確的軌道上,composer dump-autoload這將重新生成自動加載文件,幷包含您剛添加的播種機。