How to seed a pivot table in Laravel

Published 1 month ago by afoysal

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 ?

bobbybouwmann

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);
afoysal

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

Jerome2116

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.

afoysal

Thanks @Jerome2116 . Could you please help me with some links ? Thanks.

Snapey
Snapey
1 month ago (914,295 XP)

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

Snapey
Snapey
1 month ago (914,295 XP)

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.

bobbybouwmann

@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

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

bobbybouwmann
Jerome2116

And here is the series I was referring to in my message. Should go nicely with the link @bobbybouwmann sent you.

https://laracasts.com/series/advanced-eloquent

Please sign in or create an account to participate in this conversation.