有条件的多语言 laravel 播种机
conditional multi-lingual laravel seeder
假设我有一个 table,其中包含一组多种语言的数据和一个引用语言 table 上的 code
的外键 lang_code
。
现在我必须根据 lang_code
(en,es)播种数据 table。
如何根据分配的外键值使条件播种机播种,即如果 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')
]);
}
}
好的很简单,但我认为使用 Model Factory 会更容易。
$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')
]);
}
}
假设我有一个 table,其中包含一组多种语言的数据和一个引用语言 table 上的 code
的外键 lang_code
。
现在我必须根据 lang_code
(en,es)播种数据 table。
如何根据分配的外键值使条件播种机播种,即如果 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')
]);
}
}
好的很简单,但我认为使用 Model Factory 会更容易。
$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')
]);
}
}