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

Prz*_*tas 3 faker laravel laravel-5 laravel-5.5

我有这样的模型:

Entity - entities table
Address - address table
User - users table
Geoloc - geoloc table
UserEntity - entity_user table
Run Code Online (Sandbox Code Playgroud)

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

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

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

因此,当创建用户时,它还会创建实体、地址和 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;
        }
    ];
});
Run Code Online (Sandbox Code Playgroud)

实体工厂:

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;
        }
    ];
});
Run Code Online (Sandbox Code Playgroud)

用户工厂:

$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),
    ];
});
Run Code Online (Sandbox Code Playgroud)

数据库浏览器:

    factory(App\User::class, 30)->create();
    factory(App\Address::class, 30)->create();
Run Code Online (Sandbox Code Playgroud)

用户关系:

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

实体关系:

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

Cam*_*ilo 6

我认为您可以在播种机中实现这一点,例如:

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);
});
Run Code Online (Sandbox Code Playgroud)

如果要为每个用户创建多个实体,则需要使用saveMany()代替save()