1
votes

I am trying to add the class 'active' if tis the first item in a foreach but I keep getting a parse error? I am using Laravel and Blade, this is how I pass $members

public function getView()
    {
        Cache::remember('members.members', 1, function() {
            return Player::orderBy('id', 'DESC')->where('display_member', '=', '1')->limit(10)->get();
        });

        return view('frontend.members')->withMembers(Cache::get('members.members'));
    }

Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH), expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF)

<div class="tab-content">
    @if ($members->count() > 0)
        @foreach ($members as $key => $member)
        <div role="tabpanel" class="tab-pane @if($members->first() == $member)active@endif" id="{{ $member->username }}">
            <div class="col-md-12">
                <h1>{{ $member->username }}</h1>
                <br>
                <p>Personal Website: <a href="{{ $member->personal_website }}">{{ $member->personal_website }}</a></p>
                <p>Last Login: 1st January, 2017</p>
            </div>
        </div>
        @endforeach
    @endif
</div>
2
the above answer is great, but if u are using a different version you can change $loop->first with the $key == 0Achraf Khouadja

2 Answers

3
votes

If your version laravel 5.3:

@foreach($array as $key => $value)
    @if ($loop->first)
        ...
    @endif
@endforeach
1
votes
@foreach($array as $key => $value)
 @if ($key == 0)
    .....
 @endif
@endforeach