I'm building a site with localizations using laravel. Trying to store names and texts in 3 languages using a form. I have a model for each article and another for article translations. Using inputs for names and texts with input names like name_en, text_en, name_de, text_de etc... But i can't figure out how to pass input values to a foreach loop in the store method in my controller.
I tried to pass (Request $request) object into foreach loop but it returns an error. Code is below:
public function store(Request $request)
{
$test = new Test;
$test->isActive = true;
$test->save();
//TRANSLATED INPUTS = name_tr,text_tr,name_en,text_en,name_de,text_de
foreach (['tr', 'en', 'de'] as $locale => $request)//OBVIOUSLY WRONG
{
$test->translateOrNew($locale)->name = $request->input('name_'.$locale);
$test->translateOrNew($locale)->text = $request->input('body_'.$locale);
}
$test->save();
dd($test);
//echo 'Created new article with some translations!';
}
Trying to get translated inputs itno database.
foreachalready has access to$requestfrom the dependency injection, you don't need to include it as a value in yourforeachblock at all