How to seed a pivot table in Laravel

Posted 9 months 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.



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,



use Illuminate\Database\Seeder;

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

Now how can I seed those 3 tables ?

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

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.