Search code examples
arrayslaraveltranslationlaravel-5.3

Laravel 5.3: Use a array field from a controller in the view for trans()


I have a form, which is sending the data of the form to a controller using POST.

The controller returns the array of data using this line:

return view('pages.result', compact('request'))

The view displays me the correct array with all data, when I use this code in the view:

<?php $input = $request->all(); echo "<pre>"; print_r($input); echo "</pre>"; ?>

Now I want to use the field "name" of the array in a translation. The following line of code...

{{ trans('auth.resultMessage', ['name' => '<?php echo $result['name']; ?>']) }}

...displays this:

{{ trans('auth.resultMessage', ['name' => 'Max']) }}

Unfortunately, it doesn't use the translation text. It should look like this:

Everything worked fine, Max.

I've also tried the following solution, but this ends with the error Parse error: syntax error, unexpected '}', expecting ',' or ')':

{{ trans('auth.resultMessage', ['name' => '{{ Request::input("name") }}']) }}

How can I use the array field in the translation?


Solution

  • If I understood your question correctly, you should just supply the variable without trying to echoing it. Like this, for instance:

    {{ trans('auth.resultMessage', ['name' => $request->name]) }}