laravel - 未找到“播种机”类
问题描述
我已经创建了迁移并将它们迁移到数据库。现在准备创建播种机,但php artisan db:seed
给出了这个错误Class 'Seeder' not found
。
如何创建播种机类以及它存储在哪里?
我正在关注本教程https://scotch.io/tutorials/a-guide-to-using-eloquent-orm-in-laravel并且正处于我们“用关系播种数据库”的地步。
这是我的DatabaseSeeder.php
// app/database/seeds/DatabaseSeeder.php
<?php
use App\Bear;
use App\Tree;
use App\Fish;
use App\Picnic;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
// call our class and run our seeds
$this->call('BearAppSeeder');
$this->command->info('Bear app seeds finished.'); // show information in the command line after everything is run
}
}
// our own seeder class
// usually this would be its own file
class BearAppSeeder extends Seeder
{
public function run()
{
// clear our database ------------------------------------------
DB::table('bears')->delete();
DB::table('fish')->delete();
DB::table('picnics')->delete();
DB::table('trees')->delete();
DB::table('bears_picnics')->delete();
// seed our bears table -----------------------
// we'll create three different bears
// bear 1 is named Lawly. She is extremely dangerous. Especially when hungry.
$bearLawly = Bear::create(array(
'name' => 'Lawly',
'type' => 'Grizzly',
'danger_level' => 8
));
// bear 2 is named Cerms. He has a loud growl but is pretty much harmless.
$bearCerms = Bear::create(array(
'name' => 'Cerms',
'type' => 'Black',
'danger_level' => 4
));
// bear 3 is named Adobot. He is a polar bear. He drinks vodka.
$bearAdobot = Bear::create(array(
'name' => 'Adobot',
'type' => 'Polar',
'danger_level' => 3
));
$this->command->info('The bears are alive!');
// seed our fish table ------------------------
// our fish wont have names... because theyre going to be eaten
// we will use the variables we used to create the bears to get their id
Fish::create(array(
'weight' => 5,
'bear_id' => $bearLawly->id
));
Fish::create(array(
'weight' => 12,
'bear_id' => $bearCerms->id
));
Fish::create(array(
'weight' => 4,
'bear_id' => $bearAdobot->id
));
$this->command->info('They are eating fish!');
// seed our trees table ---------------------
Tree::create(array(
'type' => 'Redwood',
'age' => 500,
'bear_id' => $bearLawly->id
));
Tree::create(array(
'type' => 'Oak',
'age' => 400,
'bear_id' => $bearLawly->id
));
$this->command->info('Climb bears! Be free!');
// seed our picnics table ---------------------
// we will create one picnic and apply all bears to this one picnic
$picnicYellowstone = Picnic::create(array(
'name' => 'Yellowstone',
'taste_level' => 6
));
$picnicGrandCanyon = Picnic::create(array(
'name' => 'Grand Canyon',
'taste_level' => 5
));
// link our bears to picnics ---------------------
// for our purposes we'll just add all bears to both picnics for our many to many relationship
$bearLawly->picnics()->attach($picnicYellowstone->id);
$bearLawly->picnics()->attach($picnicGrandCanyon->id);
$bearCerms->picnics()->attach($picnicYellowstone->id);
$bearCerms->picnics()->attach($picnicGrandCanyon->id);
$bearAdobot->picnics()->attach($picnicYellowstone->id);
$bearAdobot->picnics()->attach($picnicGrandCanyon->id);
$this->command->info('They are terrorizing picnics!');
}
}
收到错误后,我尝试添加一个名为Seeder.php
并添加以下内容的模型:
// app/models/seeder.php
<?php
class Seeder extends Seeder
{
protected $fillable = [];
protected $table = 'bear';
}
我已经尝试了在这里找到的所有答案,但也composer dump-autoload
没有快乐。
解决方案
您需要确保导入Seeder
您的类扩展的 Illuminate 类
use App\Bear;
use App\Tree;
use App\Fish;
use App\Picnic;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
// Rest of code
在您的班级中名列前茅
推荐阅读
- windows - 如何使用记事本运行 cmd 命令 netsh
- tensorflow - 无法在 AWS EC2 上使用 GPU 运行 Keras
- mysql - 查询生成列的总和
- node.js - Express/NodeJS + Mongoose App 服务器响应慢
- javascript - 从 PLYLoaders 将两个几何图形合并为一个几何图形
- vue.js - 对子组件的指令
- amp-html - 如何为所有 amp-img 设置布局?
- javascript - 在设置下拉值之前调用 javascript 函数
- javascript - 保证不为我工作 http.get
- spring - 无法在 Spring Batch Tasklet 中使用 @PersistenceContext 创建自定义存储库的 @Bean?