Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seed a pivot table using factories in Laravel

I'm new to Laravel and I'm 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 real Id's of the Users table and real Id's of Skills table to seed the pivot table? I want to do it randomly, but I don't want random numbers that doesn't match to any id. I want the Id to match with 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' => ..?
    ];
});
like image 822
alexhoma Avatar asked Oct 02 '16 16:10

alexhoma


People also ask

How do I name a pivot table in Laravel?

Laravel's naming convention for pivot tables is snake_cased model names in alphabetical order separated by an underscore. So, if one model is Feature , and the other model is Product , the pivot table will be feature_product .

What is difference between factory and seeder in Laravel?

Database seeder is used to populate tables with data. Model factories is a convenient centralized place to define how your models should be populated with fake data.

How do I create a pivot table in Laravel 8?

You have to run - php artisan make:migration create_project_user_table --create --table=project_user to create the pivot table migration file after that you can copy/paste code in this migration file and run migrate command to create pivot table in your database.


2 Answers

For those who are using laravel 8.x and are looking for a solution to a problem like this;

In laravel 8.x you can feed your pivot using Magic Methods, For example if you have a belongsToMany relation named "userSkills" in User model, you should feed the pivot table this way:

User::factory()->hasUserSkills(1, ['skills' => 'draw'])->create();

You can find the documentation here

like image 192
Mina Taftian Avatar answered Sep 22 '22 13:09

Mina Taftian


I do not think that this is the best approach but it works for me.

$factory->define(App\UserSkill::class, function (Faker\Generator $faker) {
    return [
        'user_id' => factory(App\User::class)->create()->id,
        'skill_id' => factory(App\Skill::class)->create()->id,
    ];
});

If you do not want to create a model just for the pivot table, you can insert it manually.

DB::table('user_skill')->insert(
    [
        'user_id' => factory(App\User::class)->create()->id,
        'skill_id' => factory(App\Skill::class)->create()->id,
    ]
);

Or, with random existing values.

DB::table('user_skill')->insert(
    [
        'user_id' => User::select('id')->orderByRaw("RAND()")->first()->id,
        'skill_id' => Skill::select('id')->orderByRaw("RAND()")->first()->id,
    ]
);
like image 32
Rafael Berro Avatar answered Sep 22 '22 13:09

Rafael Berro