如何在Laravel 5.4中播种透视表? [英] How to seed pivot table in Laravel 5.4?
问题描述
我正在关注Jeffrey Way的laracast中称为Incremental API的教程.
I am following a tutorial called Incremental API in laracasts by Jeffrey Way.
Laravel 4伪造者类种子和laravel 5.4之间有不同的编码.
There is a different coding between Laravel 4 faker class seeding and laravel 5.4.
我仍然遵循教程"Seeders Reloaded"中的相同代码行.现在,我受困于"Class LessonTagTableSeeder不存在"
I still followed the same code lines from the tutorials "Seeders Reloaded". Now, I am stuck with "Class LessonTagTableSeeder does not exist"
TagTableSeeder
TagTableSeeder
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(),
]);
}
}
LessonTagTableSeeder
LessonTagTableSeeder
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)
]);
}
}
DatabaseSeeder
DatabaseSeeder
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
当我运行"php artisan db:seed --class = LessonTagTableSeeder"时,系统提示我:
When i run "php artisan db:seed --class=LessonTagTableSeeder" , i am prompted with:
[ReflectionException]类LessonTagTableSeeder不存在
您知道如何编辑上面的代码吗?感谢您的帮助
Do you have any idea how to edit the code above? Any help is appreciated
推荐答案
运行此命令,然后重试
composer dump-autoload -o
composer dump-autoload -o
这篇关于如何在Laravel 5.4中播种透视表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!