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 ?