Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seed pivot table in Laravel 5.4?

Tags:

php

mysql

laravel

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

like image 476
redshot Avatar asked Apr 22 '17 02:04

redshot


2 Answers

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)

like image 74
Prashant Barve Avatar answered Oct 19 '22 02:10

Prashant Barve


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.

like image 43
Alexey Mezenin Avatar answered Oct 19 '22 04:10

Alexey Mezenin