如何在Laravel 5.4中种植数据透视表?

问题描述 投票:7回答:5

我正在跟随Jeffrey Way在laracasts中使用名为Incremental API的教程。

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

我仍然遵循教程“Seeders Reloaded”中的相同代码行。现在,我被困在“Class 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');

    }

我能够使用php artisan db:seed --class = TagsTableSeeder播种TagsTableSeeder

当我运行“php artisan db:seed --class = LessonTagTableSeeder”时,系统会提示我:

[ReflectionException]类LessonTagTableSeeder不存在

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

php mysql laravel
5个回答
1
投票

运行此命令,然后再试一次

composer dump-autoload -o


8
投票

当您对播种器文件进行更改并且它不反映您的更改时,您需要运行composer dump autoload。

您可以使用以下任一命令

$ composer dump-autoload

$ composer du

$ composer dump


$ composer dump-autoload -o

然后尝试再次运行命令db:seed,它会反映您的更改。

composer dump autoload是做什么的?

composer dump-autoload不会下载任何东西。它只是重新生成需要包含在项目中的所有类的列表(autoload_classmap.php)。非常适合在项目中使用新课程。

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


3
投票

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

composer du

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


0
投票
Usually cache 

php artisan cache:clear

composer update

php artisan serve

0
投票

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

您为“DatabaseSeeder”提供了3条关键代码:

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

根据您编写的内容,您只运行'TagsTableSeeder'和'LessonTagTableSeeder'的命令。您错过了运行'LessonsTableSeeder'的命令。

换句话说,您在“标签”表中有记录,但在“课程”表中没有记录。因此,两个表之间没有关联的记录,SQL无法创建空关联(数据透视)表。

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

© www.soinside.com 2019 - 2024. All rights reserved.