我可以将数据从播种机传递到工厂吗?
我正在构建一个需要在开始时添加数据的应用程序
//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]
];
答案 0 :(得分:1)
我假设您要生成除工厂名称以外的随机数据,默认情况下仅保留工厂并覆盖“名称”
例如(使用默认的UserFactory Laravel附带)
然后在播种机中
document.querySelector('.side-nav').addEventListener('click', e => {
let el = e.target;
// The loop will stop when there's a page dataset
// Also will stop when the element that is checking is .side-nav (event.currentTarget), as there's no need to check beyond that.
while(el && el != event.currentTarget && !el.dataset.hasOwnProperty("page")) {
el = el.parentNode;
}
let page = el && el.dataset.page;
if (page) {
console.log(page);
}
});
结果
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]);
}
}
希望这会有所帮助