我正在学习 Jeffrey Way 在 laracasts 中编写的名为增量 API 的教程。
Laravel 4 faker class seeding 和 laravel 5.4 之间有不同的编码。
我仍然遵循教程“Seeders Reloaded”中的相同代码行。现在,我陷入了“Class LessonTagTableSeeder does not exist”
标签表播种器
class TagsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker::create('App\Tag');
for($i=1; $i <= 10; $i++) {
DB::table('tags')->insert([
'name' => $faker->word,
'created_at' => \Carbon\Carbon::now(),
'updated_at' => \Carbon\Carbon::now(),
]);
}
}
类(class)标签表播种器
use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
use App\Lesson;
use App\Tag;
class LessonTagTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker::create();
$lessonIds = Lesson::pluck('id')->all();
$tagIds = Tag::pluck('id')->all();
for($i=1; $i <= 30; $i++) {
DB::table('lesson_tag')->insert([
'lesson_id' => $faker->randomElement($lessonIds),
'tag_id' => $faker->randomElement($tagIds)
]);
}
}
数据库播种器
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Lesson;
use App\Tag;
use DB;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');
Lesson::truncate();
Tag::truncate();
DB::table('lesson_tag')->truncate();
Model::unguard();
$this->call('LessonsTableSeeder');
$this->call('TagsTableSeeder');
$this->call('LessonTagTableSeeder');
DB::statement('SET FOREIGN_KEY_CHECKS=1');
}
我能够使用 php artisan db:seed --class=TagsTableSeeder 为 TagsTableSeeder 播种
当我运行“php artisan db:seed --class=LessonTagTableSeeder”时,系统提示我:
[反射异常] LessonTagTableSeeder 类不存在
你知道如何编辑上面的代码吗?感谢任何帮助
最佳答案
当您对种子文件进行更改并且它没有反射(reflect)您的更改时,您需要运行 composer dump autoload。
您可以使用以下任一命令
$ composer dump-autoload
$ composer du
$ composer dump
$ composer dump-autoload -o
然后尝试再次运行您的命令 db:seed,它会反射(reflect)您所做的更改。
什么是 composer dump autoload?
composer dump-autoload
不会下载任何东西。它只是重新生成需要包含在项目中的所有类的列表 (autoload_classmap.php)。当您的项目中有新类时的理想选择。
理想情况下,您执行 composer dump-autoload -o
,以便更快地加载您的网页。它不是默认值的唯一原因是生成它需要更长的时间(但只是稍微引人注目)
关于php - 如何在 Laravel 5.4 中播种数据透视表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43554597/