Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
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

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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)

about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report

0

run this command and then try again

composer dump-autoload -o

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!