I'm new to Laravel and looking for a good way to seed a pivot table using factories. I don't want to use plain seeders. I'll show you the case:
I have three tables (users, skills, and user_skill).
users user_skill skills
+----------------+ +----------------------+ +-----------------+
| id | name | | user_id | section_id | | id | skills |
+----------------+ +----------------------+ +-----------------+
| 1 | Alex | | | | | 1 | draw |
|----------------| |----------------------| |-----------------|
| 2 | Lucy | | | | | 2 | program |
|----------------| |----------------------| |-----------------|
| 3 | Max | | | | | 3 | social |
|----------------| |----------------------| +-----------------+
| 4 | Sam | | | |
+----------------+ +----------------------+
Is there a good way to take reals Id
's of the Users table and reals Id
's of the Skills table to seed the pivot table? I want to do it randomly, but I don't want random numbers that don't match any ID. I want the ID to match the users and skills.
I don't know how to start, and I'm looking for a good example. Maybe something like this?
$factory->defineAs(App\User::class, 'userSkills', function ($faker) {
return [
'user_id' => ..?
'skills_id' => ..?
];
});
UserSkill
for the pivot tableuser_skill
? And this is not necessary right? There is no way to seed that sable just taking the currentid
's of theusers
andskills
tables? – Souvenir