0

我想在不对语言环境进行硬编码的情况下制作可翻译模型的工厂。

我必须播种两个值,slug 是不可翻译的,而 title 必须从语言表中翻译成所有语言。

这是工厂文件:

$factory->define(App\Category::class, function (Faker $faker) {

 $counter = 1;
 $locales = Language::pluck('lang'); //returns hr,en,de
 $titles = [];

 foreach ($locales as $locale) {
    $titles[$locale] = [
        'title' => 'Title for category-' .$counter++. ' on '. $locale . ' language'
    ];
 }

/*
$titles = 
     "hr" => array:1 [
        "title" => "Title for category-1 on hr language"
     ]
     "en" => array:1 [
        "title" => "Title for category-2 on en language"
      ...
 */

return [
    'slug' => 'category-'.$counter++,
    $titles
];
});

这给了我一个错误:

未找到列:1054 未知列 'hr'

工厂创建的查询:

插入到category_translations( locale, hr, en, de, test, category_id) 值中 (0, hr 语言中第 1 类的标题, en 语言中第 2 类的标题, de 语言中第 3 类的标题, 测试语言中第 4 类的标题, 20))

4

1 回答 1

0

经过一些实验,我找到了一个解决方案,这里是:

$factory->define(App\Category::class, function (Faker $faker) {

 static $counter = 1;
 $locales = Language::pluck('lang');
 $titles = array('slug' => 'CATEGORY-'.$counter);

 foreach ($locales as $locale) {
    $titles[$locale] = [
        'title' => 'Title for category-' .$counter. ' on '. $locale . ' language'
    ];
 }
 $counter++;


return $titles;
});
于 2018-07-01T19:25:45.227 回答