How to sow pivot table in Laravel 5.4?

I am following a tutorial called "Incremental API" at Jeffrey Path Laracas.

There is a different coding between visiting the Faker Laravel 4 class and laravel 5.4.

I continued to stick to the same code lines from Seeders Reloaded tutorials. Now I'm stuck in "LessonTagTableSeeder class 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 managed to load the TagableSeeder tag using php artisan db: seed --class = TagsTableSeeder

When I run "php artisan db: seed -class = LessonTagTableSeeder", they tell me:

[ReflectionException] LessonTagTableSeeder class does not exist

- ?

+6
5

,

dump-autoload -o

+1

, .

$ composer dump-autoload

$ composer du

$ composer dump


$ composer dump-autoload -o

db: seed .

?

composer dump-autoload . , (autoload_classmap.php). , .

dump-autoload -o -. , , , ( )

+6

, LessonTagTableSeeder.php , . :

composer du

.

+2
Usually cache 

php artisan cache:clear

composer update

php artisan serve
0

- , , , " ".

3 , "DatabaseSeeder":

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

, , "TagTableSeeder" "LessonTagTableSeeder". "LessonsTableSeeder".

, "Tag", "Lesson" . , , SQL ( ).

, . seed . , .

0

Source: https://habr.com/ru/post/1016690/


All Articles