我正在尝试使用相关图像播种用户。每个用户最多可以拥有5张图片。图像表的位置字段是0-4的整数。
有我的代码:
factory(App\User::class, 50)->create()->each(function($user) {
$user->images()->saveMany(factory(\App\Image::class, rand(2, 5))->make());
});
$factory->define(Goods\Image::class, function(Faker\Generator $faker) {
return [
'id' => $faker->unique()->uuid,
'position' => ????
];
});
如何设置其值将从0-4增加的位置值(根据图像编号)。
我也试过这个,但位置一直保持为0:
factory(App\User::class, 50)->create()->each(function($user) {
$position = 0;
$user->images()->saveMany(factory(\App\Image::class, rand(2, 5))->make([
'position' => $position++
]));
});
答案 0 :(得分:0)
尝试某事:
$position = 0;
factory(App\User::class, 50)->create()->each(function($user) {
$user->images()->saveMany(factory(\App\Image::class, rand(2, 5))->make([
'position' => $position++
]));
});