假设我有一个表,其中包含多种语言的数据集和引用语言表上lang_code
的外键code
。
现在我必须基于lang_code
(en,es)播种数据表。
如何根据分配的外键值使条件播种机播种,即如果lang_code
是en
然后用英语播种,依此类推?
这是我在dataTableSeeder上的内容
public function run(Faker $faker)
{
$fakerEs = Faker\Factory::create('es_ES');
$instructionIDs = DB::table('oes_instructions')->pluck('id')->toArray();;
$languageIDs = DB::table('oes_languages')->pluck('code')->toArray();;
foreach (range(1,4) as $index) {
DB::table('oes_exam_instructions_data')->insert([
'instruction_id' => $faker->randomElement($instructionIDs),
'locale' => $faker->randomElement($languageIDs),
'content' => $faker->paragraphs(10, $asText = true),
//Here ho do i make this based on $languageIDs => $faker->paragraphs(10, $asText = true)
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
]);
}
}
最佳答案
好的很简单,但是我认为使用模型工厂会更容易。
$fakerEs = Faker\Factory::create('es_ES');
$instructionIDs = DB::table('oes_instructions')->pluck('id')->toArray();
$languageIDs = DB::table('oes_languages')->pluck('code')->toArray();
foreach ($instructionIDs as $instructionID) {
foreach ($languageIDs as $languageID) {
$data = ($languageID == 'en') ? $faker->paragraphs(6, $asText = true) : $fakerEs->paragraphs(6, $asText = true);
DB::table('oes_instructions_data')->insert([
'instruction_id' => $instructionID,
'locale' => $languageID,
'content' => $data,
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
]);
}
}
关于php - 有条件的多语种幼虫播种机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48689241/