I want to factory seed a translatable model without hardcoding the locales.
There are two values that I have to seed, slug which is not translatable, and title which has to be translated to all the languages from the languages table.
Here is the factory file:
$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
];
});
This gives me an error:
Column not found: 1054 Unknown column 'hr'
Query created by the factory:
insert in to
category_translations
(locale
,hr
,en
,de
,test
,category_id
) values (0, Title for category-1 on hr language, Title for category-2 on en language, Title for category-3 on de languag e, Title for category-4 on test language, 20))
After some experimenting I've found a solution, here it is:
$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;
});