我可以将数据从播种机传递到工厂吗?
我正在构建一个需要在开始时添加数据的应用程序
//seeder
public function run()
{
$id = 0 //can i pass this var to factory?
factory(App\VehicleCategory::class, 16)->create();
}
//factory
$factory->define(VehicleCategory::class, function (Faker $faker) {
$data = [
'MICRO','SEDAN','CUV','SUV','MINIVAN','HATCHBACK','ROADSTER',
'PICKUP','VAN','COUP'TRUCK','BIG TRUCK'
];
return [
//
'name' => $data[$id]
];
$id++;
});
我试图绕过这个也不能用
试图这样做,但总是返回相同的ID
$id = 0
$data = [
'MICRO','SEDAN','CUV','SUV','MINIVAN','HATCHBACK','ROADSTER',
'PICKUP','VAN','COUP'TRUCK','BIG TRUCK'
];
if(VehicleCategory::count() != 0){
$id = VehicleCategory::latest()->first()->id;
}
return [
//
'name' => $data[$id]
];
我假设您要生成除工厂名称以外的随机数据,默认情况下只需保留工厂并覆盖'名称'
例如(使用默认的UserFactory Laravel附带)
然后在播种机中
public function run()
{
$data = ['MICRO', 'SEDAN', 'CUV', 'SUV', 'MINIVAN', 'HATCHBACK', 'ROADSTER', 'PICKUP', 'VAN', 'COUP', 'TRUCK', 'BIG TRUCK'];
foreach ($data as $name) {
factory(User::class)->create(['name' => $name]);
}
}
[
{
"id": 1,
"name": "MICRO",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 2,
"name": "SEDAN",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 3,
"name": "CUV",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 4,
"name": "SUV",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 5,
"name": "MINIVAN",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 6,
"name": "HATCHBACK",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 7,
"name": "ROADSTER",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 8,
"name": "PICKUP",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 9,
"name": "VAN",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 10,
"name": "COUP",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 11,
"name": "TRUCK",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
},
{
"id": 12,
"name": "BIG TRUCK",
"email": "[email protected]",
"email_verified_at": "2019-09-29 17:49:36",
"created_at": "2019-09-29 17:49:36",
"updated_at": "2019-09-29 17:49:36"
}
]
希望这会有所帮助