0

In a blade template y controller passes a object which has few values in properties as bellow

value_L1 = "Hello"
value_L2 = "Stack"
value_L3 = "OverFlow"

I want to use a for loop to display these values but the statement in loop returns null, how can I do such a thing in blade?

@for ($count = 1; $count < 3; $count++)
    {{ $object->{'value_L.$count'} }}
@endfor

I am able to access these properties as following

{{ $object->value_L1 }}
{{ $object->value_L2 }}

Thanks,

K

4
  • 3
    Single quotes will treat strings as literals, meaning your $count will be treated as a string $count not as the variable $count. Have a look here. Commented Oct 16, 2015 at 15:26
  • Plus he has a dot between the L and the $count Commented Oct 16, 2015 at 15:28
  • I saw this to access a property using a variable $object->{'$var'} where $var="value_L1" that worked. Commented Oct 16, 2015 at 15:30
  • Doesn't $object->{'value_L'.$count} work? Commented Sep 14, 2017 at 7:03

1 Answer 1

3

How about

@for ($count = 1; $count < 3; $count++)
    {{ object_get($object, "value_L{$count}" ) }}
@endfor
Sign up to request clarification or add additional context in comments.

9 Comments

Excellent! Can you please mark it as correct answer?
Yes I will, I am testing his edit, and also when i try @if (isset(object_get($object, "value_L{$count}" ))) it gives syntax error
do you have @endif?
When I search on object_get I see no info on php.net or laravel documentation. Does it return false or null if property it is trying to access isn't available?
Yes I have @endif, in fact I have a @else followed by @endif
|

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.