2014-10-01 45 views
136

當我運行PHP 工匠DB:種子我收到以下錯誤:Laravel 5 - 工匠種子[ReflectionException]類SongsTableSeeder不存在

[ReflectionException] Class SongsTableSeeder does not exist 

這是怎麼回事?

DatabaseSeeder類:

<?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('SongsTableSeeder'); 
    } 

} 

SongsTableSeeder類:

<?php 

// Composer: "fzaninotto/faker": "v1.4.0" 
use Faker\Factory as Faker; 
use Illuminate\Database\Seeder; 
use DB; 

class SongsTableSeeder extends Seeder { 

    public function run() 
    { 
     $faker = Faker::create(); 
     $songs = []; 
     foreach(range(1, 10) as $index) 
     { 
      $songs[] = ['title' => $faker->words(rand(1,4))]; 
     } 

     DB::table('songs')->insert($songs); 

    } 

} 
+40

'composer dump-autoload'? – 2014-10-01 14:09:01

+0

我認爲**使用Faker \ Factory作爲Faker; **正在做這項工作? – Sasha 2014-10-01 14:18:22

+0

包含'SongsTableSeeder'類定義的文件的路徑是什麼? – 2014-10-01 14:43:35

回答

370

你需要把SongsTableSeeder到文件SongsTableSeeder.php在你有你的DatabaseSeeder.php文件同一目錄下。

而你需要在控制檯上運行:

composer dump-autoload 

產生新的類映射,然後運行:

php artisan db:seed 

我只是測試它。它的工作沒有問題Laravel 5

+3

不適合我,仍然得到'反射例外'。 – KillABug 2015-03-31 09:17:57

+1

我也是。這太令人沮喪了。 – 2015-05-09 16:42:41

+1

我沒有在我的播種班上使用模型。現在工作正常。 – 2015-05-09 16:46:38

3

我運行了最新的Laravel 5開發版,如果你已經改變了命名空間,你需要打電話給你的種子類是這樣的:

$this->call('\todoparrot\TodolistTableSeeder'); 

顯然你需要用你指定的命名空間替換todoparrot。否則,我會收到原始問題中指出的相同錯誤。

0

如果我們CustomTableSeeder是同一個目錄DatabaseSeeder我們應該使用如下:

$this->call('database\seeds\CustomTableSeeder'); 
我們DatabaseSeeder文件

; 那麼另一個錯誤會被髮出,說:「DB類沒有發現」 那麼,我們應該我們的DB門面添加到我們的CustomTableSeeder文件象下面這樣:

use Illuminate\Support\Facades\DB; 

它爲我工作!

0

我只用單個文件有以下兩類它:

use Illuminate\Database\Seeder; 
use Illuminate\Database\Eloquent\Model; 
use App\Lesson; 

use Faker\Factory as Faker; 

class DatabaseSeeder extends Seeder { 

/** 
* Run the database seeds. 
* 
* @return void 
*/ 
public function run() 
{ 
    //Lesson::truncate(); 

    Model::unguard(); 

    $this->call("LessonsTableSeeder"); 


} 

} 

class LessonsTableSeeder extends Seeder { 

/** 
* Run the database seeds. 
* 
* @return void 
*/ 
public function run() 
{ 

    $faker = Faker::create(); 

    foreach(range(1,30) as $index) { 

     Lesson::create(['title' => $faker->sentence(5), 'body' => $faker->paragraph(4)]); 

    } 

} 

} 
0

我有同樣的「反射異常」錯誤。解決的辦法是將類文件從dev中複製到服務器上。愚蠢的錯誤,但鑑於我們處理了多少個文件,每次都很容易忘記將它們複製到服務器上。

10

我解決它通過這樣做:

  1. 複製文件內容。
  2. 刪除文件。
  3. 運行命令:php artisan make:seeder。
  4. 將文件內容複製回此文件中。

發生這種情況是因爲我對文件名進行了更改。我不知道爲什麼改變後它不起作用。

+1

可能運行composer dump-auto會更容易。 自動加載器現在正在查找較舊的文件。所以如果你通過命令行轉儲這個文件,它將會用新文件生成一個新的自動加載器文件。 所以在未來嘗試這= = – 2016-04-06 13:51:49

+0

有關作曲家自動裝載機的更多信息,請參閱本網站: https://getcomposer.org/doc/01-basic-usage.md#autoloading – 2016-04-06 14:31:29

+0

van Asseldok,好吧,'作曲家轉儲-autoload'不適用於我。我必須重新創建播種類以使其工作。 – 2017-11-10 09:24:38

5

文件SongsTableSeeder.php應該在數據庫/種子目錄或在其子目錄中。

你需要運行:

composer dump-autoload 

然後:

php artisan db:seed 

或:

php artisan db:seed --class=SongsTableSeeder 
+1

能夠看到**解析錯誤**'php artisan db:seed --class = SongsTableSeeder'命令。 thnks! – Omi 2017-10-18 07:09:58

0

Laravel需要一個 「根」 播種機類:

見工匠幫助頁面:

$ php artisan help db:seed 

Usage: 
    db:seed [options] 

Options: 
     --class[=CLASS]  The class name of the root seeder [default: "DatabaseSeeder"] 
     --database[=DATABASE] The database connection to seed 
     --force    Force the operation to run when in production. 
    -h, --help     Display this help message 
    -q, --quiet    Do not output any message 
    -V, --version    Display this application version 
     --ansi     Force ANSI output 
     --no-ansi    Disable ANSI output 
    -n, --no-interaction  Do not ask any interactive question 
     --env[=ENV]   The environment the command should run under 
    -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug 

Help: 
    Seed the database with records 

如果要刪除DatabaseSeeder,則必須使用--class選項定義播種器類。

相關問題