0

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.

1
  • The foreach already has access to $request from the dependency injection, you don't need to include it as a value in your foreach block at all Commented Sep 1, 2019 at 18:42

1 Answer 1

1

It is possible to do this the way you have it set up. You could use a series of if str contains, then str replace on name, and text, until you get the right language. But that's potentially a lot of work and likely pretty confusing if you have many names and texts coming from your form (which I assume you do from the need for a foreach on the incoming data).

I suggest you re-work your form slightly to return a bit more information. Consider the following as possible elements to return from the form:

  1. name[]
  2. text[]
  3. language[]

Then, in your store method you can run through each of these in a number of ways, but to explain, I'll use the clearest (though not the most efficient):

Set an index:

$i = 0;

Run through all of the form's returns, and get the corresponding text and language that comes with the name (through the same index):

foreach($request->get('name') as $name){
     $lang = $request['language'][$i];
     $test->translateOrNew($lang)->name = $name;
     $test->translateOrNew($lang)->text = $request['text'][$i];
     $i ++;
}

This is almost pseudo code, and you will have to re-factor to make it work for you, but it should give you one idea on how you might do this. You will need to validate that the user provides each set (name, text, language) as complete, or the index will fail.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your suggestion. I followed a different approach and instead of using a loop i kinda used a 'hardcoded' multi-dimensional array for each language and stored them in a variable then saved.
No worries, glad you got it solved. Yes, there are several ways to make this work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.