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

afoysal's avatar

How to seed a pivot table in Laravel

I have 3 tables Applicant,Skills and Applicant_skill. Here Applicant_skill is the pivot table. One Applicant has many Skills. I am trying to write factory like below.

**ApplicantSkillFactory.php**

<?php

use Faker\Generator as Faker;

$factory->define(App\Applicant_skill::class, function (Faker $faker) {
    return [
        'applicant_id' => \App\Applicant::all()->random()->id,
        'skill_id' => \App\Skills::all()->random()->id,
    ];
});

ApplicantsTableSeeder.php

<?php

use Illuminate\Database\Seeder;

class ApplicantsTableSeeder extends Seeder
{
    public function run()
    {
        factory(App\Applicant::class,20)->create()->each(function ($u) {
            $u->Applicant_skill()->associate(factory(App\Applicant_skill::class)->make());
        });
    }
}

Now how can I seed those 3 tables ?

0 likes
10 replies
bobbybouwmann's avatar

Well you can't do that with factories because it doesn't understand the relationship! Instead you need to create applications and skills and then connect them.

For example

$applications = factory(App\Applicant::class,20)->create();
$skills = factory(App\Skill::class,20)->create();

$applications->first()->skills()->sync($skills);
2 likes
afoysal's avatar

Thanks @bobbybouwmann. How can I learn more about Factory and Seeding ? Your solution is not working. Thanks.

lara56278's avatar

I would suggest you work through the Advanced Eloquent series as if memory serves me correctly, Jeffrey goes through setting up a new database with pivot tables and the Faker library and covers off seeding those tables in that series.

1 like
Snapey's avatar

I don't know if you can take any ideas from this;

This is my current projects roles and permissions seeder, similarly there is a pivot in the middle (hence the attach)

public function run()
{
    $clubAdmin =        Role::create(['name'=>'club admin']);
    $districtAdmin =    Role::create(['name'=>'district admin']);
    $systemAdmin =      Role::create(['name'=>'system admin']);

    foreach([
        'club.member.add',
        'club.member.delete',
        'club.member.edit',
        'club.give.admin',
        'club.officer.manage',
        'club.manage',
            ] as $permission) {

            $perm = Permission::create(['name' => $permission]);

            $clubAdmin->permissions()->attach($perm);
            $districtAdmin->permissions()->attach($perm);
        }

}

I create the permission and then attach it to the role

1 like
Snapey's avatar

or creating members and attaching a random set of badges (like your skills). There is a badge_member pivot

class MembersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Member::class, 2000)->create();

        // give each member some badges
        foreach(App\Member::all() as $member) {

            foreach(App\Badge::all() as $badge) {

                if (rand(1, 100) > 70) {
                        $member->badges()->attach($badge->id);
                }
            }
            $member->save();
        }
    }
}

I create all the members in one step with factory and then step over them adding the badges. Each badge has 30% chance of being attached to a member.

4 likes
bobbybouwmann's avatar

@afoysal The code is not working? Do you get any errors.. You need to put some effort in it if you want use to help you. Just saying that is doesn't work doesn't help...

afoysal's avatar

Thanks @bobbybouwmann for your reply. I would like to learn more about Factory and Seeding. Could you please help me in this regard ? Thanks

Please or to participate in this conversation.