gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:05:37 25 4
gpt4 key购买 nike

我有这样的模型:

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

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

实体工厂:
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');
}

最佳答案

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

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

关于laravel - 如何在 Laravel 中将工厂关系添加到 faker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47351852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com