1

I have two variables, which are same but localized. And i also have a $locale variable, which is holding locale appendix (_en etc..)

Now i want to combine them and create variable variable to print proper localized variable.

{{${'$v->fuel_type->vehicle_fuel_type'}.$locale}}

But i get error Undefined variable: $v->fuel_type->vehicle_fuel_type_en (View: /var/www/html/sixt/resources/views/offerselect-new.blade.php)

But there is $v->fuel_type->vehicle_fule_type variable. What's wrong with this?

13
  • 1
    Take the single quotes out Commented Apr 10, 2017 at 13:01
  • What is the format of the variable that you're trying to generate? Commented Apr 10, 2017 at 13:02
  • @aynber json object? Commented Apr 10, 2017 at 13:04
  • Try {{ $v->fuel_type->${'vehicle_fuel_type' . $locale} }} Commented Apr 10, 2017 at 13:14
  • @Robert same error... vehicle_fuel_type_en undefined Commented Apr 10, 2017 at 13:17

1 Answer 1

1

Using the (hardly documented) laravel helper data_get would make things a lot easier for you:

{{ data_get($v, "fuel_type.vehicle_fuel_type".($locale?"_".$locale:"")) }}

This is similar to array_get but also works for objects.

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

Comments

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.