如何在Laravel中为工厂添加工厂关系

时间:2017-11-17 13:34:00

标签: laravel laravel-5 laravel-5.5 faker

我有这样的模特:

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

现在,实体可以有一个地址和一个地理位置,但它可以有多个用户,这就是UserEntity的用途。

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

使用者>实体GT;地址>&Geoloc GT; UserEntity

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

这是我目前的设置:

AddressFactory:

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

EntityFactory:

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

UserFactory:

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

DatabaseSeeder:

    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');
}

2 个答案:

答案 0 :(得分:2)

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

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()

答案 1 :(得分:1)

你应该在播种机上这样做。

例如:

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