2017-04-22 118 views
6

我在Jeffrey Way的laracasts里面学习一个名为Incremental API的教程。如何在Laravel 5.4中创建数据透视表?

Laravel 4 faker类播种和laravel 5.4之间有不同的编码。

我仍然遵循教程“Seeders Reloaded”中的相同代码行。现在,我坚持“类LessonTagTableSeeder不存在”

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

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

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'); 

    } 

我能播种TagsTableSeeder与PHP人员分贝:种子--class = TagsTableSeeder

当我运行 “PHP人员分贝:种子--class = LessonTagTableSeeder”,我提示:

[ReflectionException] 类LessonTagTableSeeder不存在

你知道如何编辑上面的代码吗?任何帮助表示赞赏

回答

1

运行此命令,然后再试

作曲家转储自动加载-o

+0

谢谢。它像一个魅力! –

2

确保该文件被命名为LessonTagTableSeeder.php,并且它与其他播种机位于同一目录中。然后运行这个命令:

composer du 

之后尝试再次执行播种机。

+0

谢谢。我的代码现在可用 –

0
Usually cache 

php artisan cache:clear 

composer update 

php artisan serve 
+3

请编辑您的答案以包含一些解释。仅有代码的答案对未来SO读者的教育很少。您的回答是在低质量的审核队列中。 – mickmackusa

6

当您更改到播种机的文件,它不会反映更改的需要运行作曲家转储自动加载。

您可以使用下面的命令中的任何一个

$ composer dump-autoload 

$ composer du 

$ composer dump 


$ composer dump-autoload -o 

然后尝试运行你指挥DB:再次种子,它反映了你的变化。

什么作曲家自动加载程序做?

composer dump-autoload不会下载东西。它只是重新生成需要包含在项目中的所有类的列表(autoload_classmap.php)。当你在你的项目中有一个新班级时,这是理想的选择。

理想情况下,您可以执行作曲家dump-autoload -o,以加快网页的载入速度。它不是默认的唯一原因是因为它需要更长的时间来生成(但只是稍微明显)

0

数据透视表或关联表是映射其他两个表之间关系的表,非常有用对于有多对多关系的两个表。

有您所提供的“DatabaseSeeder”哪些代码3条临界线:

$this->call('LessonsTableSeeder'); 
    $this->call('TagsTableSeeder'); 
    $this->call('LessonTagTableSeeder'); 

根据你写的,你只跑的命令为“TagsTableSeeder”和“LessonTagTableSeeder”。您错过了运行'LessonsTableSeeder'的命令。

换句话说,您在'标记'表中有记录,但'教训'表中没有记录。因此,两个表之间没有关联的记录,并且SQL无法创建空关联(数据透视表)表。

作为进一步说明,创建关联表时,种子操作的执行顺序很重要。在其他两个表种子后,您必须对关联表执行seed命令。关联表需要知道每个其他表中的唯一标识符以创建关系。