Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

purify's avatar

Check if forelse is empty

Hi there,

I have two for loops. One is a forelse and one is a foreach. I'm aware this may need to change.

I'm trying to combine them so that I can output data from each loop. The problem I'm having is that if the first loop is empty, it outputs that it's empty. The second loop doesn't output anything if it's empty. What I'm trying to do is so that if both loops are empty, then output the empty message. Anyone have an idea how I can do this?

@forelse($requests as $request)
                <h4 class="profile-name">{{ $opponent->username }} said hi also</h4>
                @if ($opponent->ratingActive == 1)
                    <h6 class="profile-rank"><span class="icon-rank"></span> {{ $opponent->rating }}</h6>
                @endif
        @empty
            //OUTPUT THIS IF BOTH LOOPS RETURN EMPTY-----------------------------
            <div class="game-request nn-container">
                <h4 class="no-margin nn-text">No notifications</h4>
            </div>
@endforelse

@forelse($sentUsers as $sentUser)
    @if(isset($venueExists) && $venueExists->venue)
        
        @foreach($receivedUsers as $receivedUser)
        <h4 class="profile-name">{{ $receivedUser->username }} said hi</h4>
        @endforeach

        <h5><span class="green-highlight bold">{{ $venueExists->venue }}</span></h5>

    @endif
    @empty
@endforelse
0 likes
4 replies
taijuten's avatar

Could you do a count on both $requests and $sentUsers, putting that in an if statement before the loops?

purify's avatar

That was something I did try however for some reason, it doesn't output. :(

@if($requestCount == 0 && $sentCount == 0)
            <div class="game-request nn-container">
                <h4 class="no-margin nn-text">No notifications</h4>
            </div>
@endif

taijuten's avatar

Where are you getting $requestCount and $sentCount from?

Presumably just $requests->count() and $sent->count()?

purify's avatar

Precisely. :/

Edit: I managed to figure this issue out. Thank you though. :)

Please or to participate in this conversation.