9

我是 Laravel 的新手,我正在寻找一种使用factory播种数据透视表的好方法。我不想使用普通播种机。我将向您展示案例:

我有三个表(用户技能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      |   |         |            |
+----------------+   +----------------------+

有没有一种很好的方法来获取真实Id的用户表和真实Id的技能表来播种数据透视表?我想随机做,但我不想要与任何 id 不匹配的随机数。我希望 Id 与用户技能相匹配。

我不知道如何开始,我正在寻找一个很好的例子。也许是这样的?

$factory->defineAs(App\User::class, 'userSkills', function ($faker) {
    return [
        'user_id' => ..?
        'skills_id' => ..?
    ];
});
4

3 回答 3

16

我不认为这是最好的方法,但它对我有用。

$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,
    ];
});

如果您不想仅为数据透视表创建模型,您可以手动插入它。

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

或者,使用随机的现有值。

DB::table('user_skill')->insert(
    [
        'user_id' => User::select('id')->orderByRaw("RAND()")->first()->id,
        'skill_id' => Skill::select('id')->orderByRaw("RAND()")->first()->id,
    ]
);
于 2016-10-02T16:55:10.657 回答
9

对于那些正在使用 laravel 8.x 并正在寻找此类问题的解决方案的人;

在 laravel 8.x 中,您可以使用魔术方法来提供您的数据透视表,例如,如果您在用户模型中有一个名为“userSkills”的 belongsToMany 关系,您应该以这种方式提供数据透视表:

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

你可以在这里找到文档

于 2020-10-24T07:22:45.107 回答
7

我有一个类似的问题,我在 Laravel 测试中解决了这个问题。

不需要创建新的 UserSkills 模型:

版本 Laravel 5.7

数据库

users                user_skill                               skills
+----------------+   +------------------------------------+   +-----------------+
| id  | name     |   | user_id | section_id | state_skill |   | id  | skills    |
+----------------+   +------------------------------------+   +-----------------+
| 1   | Alex     |   |         |            |             |   | 1   | draw      |
|----------------|   |----------------------|-------------|   |-----------------|
| 2   | Lucy     |   |         |            |             |   | 2   | program   |
|----------------|   |----------------------|-------------|   |-----------------|
| 3   | Max      |   |         |            |             |   | 3   | social    |
|----------------|   |----------------------|-------------|   +-----------------+
| 4   | Sam      |   |         |            |             |
+----------------+   +----------------------+-------------+

用户.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;
    
    public function skills()
    {
        return $this->belongsToMany('App\Skill')
                ->withTimestamps()
                ->withPivot('state_skill');
    }
}

数据库测试.php

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;

class DataBaseTest extends TestCase
{

    /**
     * @test
     */    
    public function test_create_user_skill()
    {
       // Create DataBase
       $users = factory(\App\User::class, 1)
           ->create()
           ->each(function ($user) {

                // Create Models Support
                $skill = factory(\App\Skill::class)->create();
                
                // Create Pivot with Parameters
                $user->skills()->attach($skill->id,[
                    'state_skill' => 'ok'
                ]);
 
            });

        // Testing
        // ...
        $this->assertTrue(true);
    }
}
于 2019-03-11T15:57:43.147 回答