如何在 Laravel 中将工厂关系添加到 faker

How to add Relationship of Factories to faker in Laravel

我有这样的模型:

Entity - entities table
Address - address table
User - users table
Geoloc - geoloc table
UserEntity - entity_user table

现在 Entity 可以有一个地址和一个 geoloc,但它可以有多个用户,这就是 UserEntity 的用途。

所以我需要做一个模式,工厂将像这样创建它:

用户>实体>地址>Geoloc>用户实体

因此,当创建用户时,它还会创建实体、地址和 geoloc,将实体 ID 关联到地址,将 geoloc id 关联到实体,并将 user_id 和 entity_id 插入用户实体。

这是我当前的设置:

地址工厂:

$factory->define(App\Address::class, function (Faker $faker) {
    return [
        'building_name' => $faker->company,
        'address' => $faker->streetAddress,
        'town' => $faker->city,
        'postcode' => $faker->postcode,
        'telephone' => $faker->phoneNumber,
        'private' => $faker->boolean($chanceOfGettingTrue = 50),
        'entity_id' => function () {
            return factory(App\Entity::class)->create()->id;
        }
    ];
});

实体工厂:

factory->define(App\Entity::class, function (Faker $faker) {
    return [
        'name' => $faker->unique()->company,
        'type' => 'Service',
        'tags' => 'mens clothes, online shopping, ladies clothes',
        'email' => $faker->safeEmail,
        'logo' => $faker->imageUrl($width = 640, $height = 480),
        'cover' => $faker->imageUrl($width = 1000, $height = 600),
        'bio' => $faker->catchPhrase,
        'main' => $faker->realText($maxNbChars = 200, $indexSize = 2),
        'twitter_business' => 'OfficialOAFC',
        'facebook_business' => 'MolinoLounge',
        'instagram_business' => 'OfficialOAFC',
        'google_places' => 'ChIJvaCBHYG3e0gRfhs0xZ1TBB8',
        'eventbrite' =>'8252340832',
        'featured' => $faker->boolean($chanceOfGettingTrue = 50),
        'url' => $faker->url,
        'video' => '6R2DIFySho4',
        'wikipedia' => 'Hackerspace',
        'contact_name' => $faker->name,
        'contact_number' => $faker->phoneNumber,
        'geoloc_id' => function () {
            return factory(App\Geoloc::class)->create()->id;
        }
    ];
});

用户工厂:

$factory->define(App\User::class, function (Faker $faker) {
    static $password;
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'avatar' => $faker->imageUrl($width = 640, $height = 480),
        //'telephone' => $faker->phoneNumber,
        'role' => rand(1, 2),
        'remember_token' => str_random(10),
    ];
});

数据库播种器:

    factory(App\User::class, 30)->create();
    factory(App\Address::class, 30)->create();

用户关系:

public function entities()
{
    return $this->belongsToMany('App\Entity', 'entity_user', 'user_id', 'entity_id');
}

实体关系:

public function address()
{
    return $this->hasOne('App\Address', 'entity_id');
}
public function _geoloc()
{
    return $this->belongsTo('App\Geoloc', 'geoloc_id', 'id');
}

您应该在播种机上执行此操作。

例如:

public function run()
{
    factory(App\Currency::class, 100)->create()->each(function ($currency) {
        $currency->variations()->saveMany(factory(App\Variation::class, 50)->make());
    });
}

我认为你可以在你的播种机中实现这一点:

factory(App\User::class, 30)->create()->each(function($user) {

    $entity = factory(App\Entity::class)->make();

    $address = factory(App\Address::class)->create([
        'entity_id' => $entity
    ]);

    $user->entities()->save($entity);
});

如果您要为每个用户创建多个实体,则需要使用 saveMany() 而不是 save()