Have you tried
foreach($array as $key => $value) {
if($key == 0)
...
}
2u3 said:
Have you tried
foreach($array as $key => $value) { if($key == 0) ... }
How stupid of me, thanks!
Rather than use a foreach loop you could just directly access the first item in the object:-
{{ $blog->entries{0}->title }}
can somebody add how to get the last element? I'm sure one can use a counter , but I don't know how ...
@port22 - if you area trying to get last element of an ARRAY, you can use Laravel's helper function:
$lastElement = last($array);
$firstElement = head($array);
2u3 said:
Have you tried
foreach($array as $key => $value) { if($key == 0) ... }
perpahs this is a solution but sometimes you don't have keys starting at 0
i found this: http://robin.radic.nl/blade-extensions/directives/foreach.html
@foreach($array as $key => $value)
@if ($loop->first)
...
@endif
@endforeach
@foreach($sidebar_menus as $sm)
@if($sidebar_menus->first() == $sm)
...
@endif
@endforeach
I know it's solved. but another option to get the first element is with first(). if it's an array wrap it in a collection otherwise collections already have it.
collect(\Blog::GetEntries(1, 5, true)->entries)->first();
although an eloquent model already returns a collection so the collect part likely isn't needed.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community