简体   繁体   中英

How to seed pivot table in Laravel 5.4?

I am following a tutorial called Incremental API in laracasts by Jeffrey Way.

There is a different coding between Laravel 4 faker class seeding and laravel 5.4.

I still followed the same code lines from the tutorials "Seeders Reloaded". Now, I am stuck with "Class LessonTagTableSeeder does not exist"

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

    }

I was able to seed TagsTableSeeder with php artisan db:seed --class=TagsTableSeeder

When i run "php artisan db:seed --class=LessonTagTableSeeder" , i am prompted with:

[ReflectionException] Class LessonTagTableSeeder does not exist

Do you have any idea how to edit the code above? Any help is appreciated

When you make changes into the seeder files and it does not reflects your changes you need to run composer dump autoload.

you can use any one of the following commands

$ composer dump-autoload

$ composer du

$ composer dump


$ composer dump-autoload -o

Then try to run you command db:seed again and it reflects you changes.

what composer dump autoload do?

composer dump-autoload won't download a thing. It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php). Ideal for when you have a new class inside your project.

Ideally, you execute composer dump-autoload -o , for a faster load of your webpages. The only reason it is not default, is because it takes a bit longer to generate (but is only slightly noticeable)

Make sure the file is named as LessonTagTableSeeder.php and it's in the same directory as the other seeders. Then run this command:

composer du

After that try to execute the seeder again.

run this command and then try again

composer dump-autoload -o

Usually cache 

php artisan cache:clear

composer update

php artisan serve

A pivot table, or association table, is a table which maps the relationship between two other tables, very useful for two tables which have a many-to-many relationship.

There are 3 critical lines of code which you supplied for the 'DatabaseSeeder':

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

Based on what you wrote, you only ran the commands for the 'TagsTableSeeder' and the 'LessonTagTableSeeder'. You missed running the command for 'LessonsTableSeeder'.

In other words, you had records in the 'Tag' table, but none in the 'Lesson' table. Therefore there were no records associated between the two tables and SQL couldn't create a null association (pivot) table.

As a further note, the order of execution for seed operations is important when creating an association table. You must execute the seed command for the association table AFTER seeding the other two tables. The association table needs to know the unique identifiers in each of the other tables in order to create the relationships.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM