laravellaravel-8laravel-localization

How to retrieve translation strings inside a php code in laravel blade template


I am trying to use the localization retrieval of string inside of php foreach loop code in blade template in laravel 8.

Inside the foreach loop, I am trying to manipulate a value called $item['label'] and equate translate the value of it using the language localization that laravel have.

Here's my current code.

@foreach ($items as $item)
    @php
    $item['label'] = "{{ __($item['label']) }}"
    @endphp
@endforeach

But I get an error of

ParseError syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

In the first place, can I use a {{ __ ('string') }} or @lang('string') inside a @php in the first place? If I can't is there any other approach for this one?

Thank you very much!


Solution

  • while using foreach you cant change its value here try this this will work if $items is array not stdClass

    @foreach ($items as $key => $item)
        @php
        $items[$key]['label'] = __($item['label']);
        @endphp
    @endforeach