Laravel 5 - 工匠种子 [ReflectionException] 类 SongsTableSeeder 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26143315/
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
Laravel 5 - artisan seed [ReflectionException] Class SongsTableSeeder does not exist
提问by Sasha
When I run php artisan db:seedI am getting the following error:
当我运行php artisan db:seed时,出现以下错误:
[ReflectionException] Class SongsTableSeeder does not exist
What is going on?
到底是怎么回事?
My DatabaseSeeder class:
我的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');
}
}
My SongsTableSeeder class:
我的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);
}
}
回答by Marcin Nabia?ek
You need to put SongsTableSeeder
into file SongsTableSeeder.php
in the same directory where you have your DatabaseSeeder.php
file.
您需要将SongsTableSeeder
文件放入文件SongsTableSeeder.php
所在的同一目录中DatabaseSeeder.php
。
And you need to run in your console:
你需要在你的控制台中运行:
composer dump-autoload
to generate new class map and then run:
生成新的类映射,然后运行:
php artisan db:seed
I've just tested it. It is working without a problem in Laravel 5
我刚刚测试过了。它在 Laravel 5 中没有问题
回答by Ron van Asseldonk
I solved it by doing this:
我通过这样做解决了它:
- Copy the file content.
- Remove file.
- Run command: php artisan make:seeder .
- Copy the file content back in this file.
- 复制文件内容。
- 删除文件。
- 运行命令: php artisan make:seder 。
- 将文件内容复制回此文件中。
This happened because I made a change in the filename. I don't know why it didn't work after the change.
发生这种情况是因为我更改了文件名。我不知道为什么它在更改后不起作用。
回答by simhumileco
File SongsTableSeeder.phpshould be in database/seedsdirectory or in its subdirectory.
文件SongsTableSeeder.php应位于database/seeds目录或其子目录中。
You need to run:
你需要运行:
composer dump-autoload
and then:
进而:
php artisan db:seed
or:
或者:
php artisan db:seed --class=SongsTableSeeder
回答by Chandrakant Ganji
SongsTableSeeder.php should be in database/seeds directory
SongsTableSeeder.php 应该在 database/seeds 目录中
Console command steps:
控制台命令步骤:
composer dump-autoload
and then:
进而:
php artisan cache:clear
and then:
进而:
php artisan optimize
and then:
进而:
php artisan db:seed
or:
或者:
php artisan db:seed --class=SongsTableSeeder
回答by abenevaut
Do not forgot that the composer dump-autoload
works in relation with the autoload
/ classmap section of composer.json
. Take care about that if you need to change seeders directory or use multiple directories to store seeders.
不要忘记composer dump-autoload
与composer.json
. 如果您需要更改播种机目录或使用多个目录来存储播种机,请注意这一点。
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
},
回答by Jason
I'm running the very latest Laravel 5 dev release, and if you've changed the namespace you'll need to call your seed class like this:
我正在运行最新的 Laravel 5 开发版本,如果您更改了命名空间,则需要像这样调用您的种子类:
$this->call('\todoparrot\TodolistTableSeeder');
Obviously you'll need to replace todoparrot
with your designated namespace. Otherwise I receive the same error indicated in the original question.
显然,您需要替换todoparrot
为您指定的命名空间。否则,我会收到原始问题中指出的相同错误。
回答by ivahidmontazer
If our CustomTableSeeder is in same directory with DatabaseSeeder we should use like below:
如果我们的 CustomTableSeeder 与 DatabaseSeeder 在同一目录中,我们应该使用如下:
$this->call('database\seeds\CustomTableSeeder');
in our DatabaseSeeder File; then another error will be thrown that says: 'DB Class not found' then we should add our DB facade to our CustomTableSeeder File like below:
在我们的 DatabaseSeeder 文件中;然后将抛出另一个错误,内容为:'DB Class not found' 然后我们应该将我们的 DB Facade 添加到我们的 CustomTableSeeder 文件中,如下所示:
use Illuminate\Support\Facades\DB;
it worked for me!
它对我有用!
回答by Vinod Joshi
I have used only SINGLE FILE with TWO classes in it following :
我只使用了以下两个类的单个文件:
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)]);
}
}
}
回答by Shahid Hussain
If you have copied the seeders files from any other project then you need to run the artisan command php artisan db:seed
otherwise it is fine.
如果您已经从任何其他项目复制了播种机文件,那么您需要运行 artisan 命令,php artisan db:seed
否则就可以了。
回答by gerald heng
i got [ReflectionException] Class Seeder does not exist
too and when i use composer dump-autoload
, i got an error preg_match(): JIT compilation failed: no more memory
when i run it.
我也得到[ReflectionException] Class Seeder does not exist
了,当我使用时composer dump-autoload
,preg_match(): JIT compilation failed: no more memory
运行它时出现错误。
What i did is that i change ;pcre.jit=1
to pcre.jit=Off
in php.ini!
You can find the path by using php --ini
in your terminal!
我所做的是我换;pcre.jit=1
到pcre.jit=Off
在php.ini中!您可以php --ini
在终端中使用找到路径!
I am using mac with php 7.3! Hope that help any of you guys out there!
我正在使用带有 php 7.3 的 mac!希望对各位大侠有所帮助!