Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SmallDreams's avatar

Seeder not giving enough data

I'm trying to make fake data, so I tried this with a for loops, but it does not give all the fields data. Could someone please tell me a way so I can give all my fields data connected with the right foreign key.

Somehow I can not wrap my head around it, any help would be highly appreciated.

            $this->faker = Faker::create();

            DB::table('users')->insert([
                'username' => "username",
                'role' => "admin",
                'password' => 'password',
                'pin' => 'pincode',
            ]);

            for ($i = 1; $i <= 19; $i++) {
                DB::table('main_gallery_names')->insert([
                    'user_id' => 1,
                    'title' => $this->faker->word,
                ]);

                for ($d = 1; $d <= 3; $d++) {
                    DB::table('sub_gallery_names')->insert([
                        'main_gallery_name_id' => $i,
                        'title' => $this->faker->word,
                        'theme' => 'dark',
                    ]);

                    for ($x = 1; $x <= 2; $x++) {
                        DB::table('sliders')->insert([
                            'sub_gallery_name_id' => $d,
                            'title' => 'Test image title ',
                            'text' => 'Test image text ',
                        ]);

                        for ($l = 1; $l <= 2; $l++) {
                            DB::table('galleries')->insert([
                                'slider_id' => $x,
                                'image' => "mountains.jpeg",
                                'video' => "",
                            ]);
                        }

                    }

                }

            }
0 likes
7 replies
Sinnbeck's avatar

Which fields are missing data? On what table?

Tray2's avatar

You can use a factory to help you

You create the definition in you factory class, in the example below it is the AuthorFactory

public function definition(): array
    {
        return [
            'first_name' => $this->faker->firstName(),
            'last_name' => $this->faker->lastName(),
            'slug' => Str::slug($this->faker->lastName() . ',' . $this->faker->firstName)
        ];
    }

Then you can call it like so

Author::factory()->create([
            'first_name' => 'Robert',
            'last_name' => 'Jordan'
        ]); 

If you don't pass any parameters it will use the faker ones.

SmallDreams's avatar

@Tray2 The thing is the faker works it will just not make data for all foreign keys

SmallDreams's avatar
SmallDreams
OP
Best Answer
Level 1

@Sinnbeck @Tray2

This is what I was trying to do sorry for the bad explanation. Thank you guys for the help and I'm sorry for wasting your time.

$this->faker = Faker::create();

            DB::table('users')->insert([
                'username' => "username",
                'role' => "admin",
                'password' => 'password',
                'pin' => 'pincode',
            ]);

            for ($i = 1; $i <= 19; $i++) {
                DB::table('main_gallery_names')->insert([
                    'user_id' => 1,
                    'title' => $this->faker->word,
                ]);
            }

            $MGN = MainGalleryName::all();

            foreach ($MGN as $key => $item) {
                for ($d = 1; $d <= 3; $d++) {
                    DB::table('sub_gallery_names')->insert([
                        'main_gallery_name_id' => $item->id,
                        'title' => $this->faker->word,
                        'theme' => 'dark',
                    ]);
                }
            }

            $SGN = SubGalleryName::all();;

            foreach ($SGN as $key => $item) {
                for ($x = 1; $x <= 3; $x++) {
                    DB::table('sliders')->insert([
                        'sub_gallery_name_id' => $item->id,
                        'title' => 'Test image title ',
                        'text' => 'Test image text ',
                    ]);
                }
            }

            $slider = Slider::all();

            foreach ($slider as $key => $item) {
                for ($l = 1; $l <= 4; $l++) {
                    DB::table('galleries')->insert([
                        'slider_id' => $item->id,
                        'image' => "mountains.jpeg",
                        'video' => "",
                    ]);
                }
            }

Please or to participate in this conversation.