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

Xron's avatar
Level 1

Hide parent div if empty data

I'm wondering how I can hide a parent div if there's no content to display there, so far I've been using jQuery to hide the empty div but as the site is large sometimes the wrapper is displayed before the jQuery hides it again.

<div class="wrapper">
    @forelse ($question->replies as $reply)
        @if ($reply->answer_id == 1)
            <div class="header">
                <h3><a href="#">{{ $reply->user->getNameOrUsername() }}</a></h3>
            </div>
        @endif
    @empty
    @endforelse
</div

How can I hide the parent wrapper if the if statement is empty

0 likes
5 replies
goatshark's avatar

@Xron: I'm sure there are better ways to do it, but if you don't mind being extra explicit about it, you could throw another @if statement around it:

@if ( count($question->replies) > 0 )
    <div class="wrapper">
        @forelse ($question->replies as $reply)
            @if ($reply->answer_id == 1)
                <div class="header">
                    <h3><a href="#">{{ $reply->user->getNameOrUsername() }}</a></h3>
                </div>
            @endif
        @empty
        @endforelse
    </div>
@endif

I've used @if(!empty($thing)) also, but not sure if that applies here.

Xron's avatar
Level 1

Hi thanks, I don't mind using another if statement but I need to make the new if statement include the answer_id == 1 is it possible to include that e.g. -

@if (count($question->replies->answer_id == 1) > 0)
goatshark's avatar

I think I got lost here. Because you're specifically looking for the answer_id to be "1", won't you always only get one result? I guess I'm making an assumption that answer_id is an id field (primary key) on a table. ?

Xron's avatar
Level 1

It's not a primary key multiple replies can have the answer_id set to 1 . This what confused me about the question

Snapey's avatar

Put the logic in your question model so that you can

@if($question->hasreplies())

Please or to participate in this conversation.