0

I want to hide the title if there is no data show in foreach result. so, where should i place the section title?

this is the code

<div class="title">
//section title
   <div class="navigation-bar">
        <h3>Title</h3>
    </div>

 @foreach($similar_posts as $related_post)
  //data
 @endforeach

</div>

thanks

Quinn
  • 697
  • 1
  • 10
  • 25
  • 2
    Possible duplicate of [Eloquent collection: counting and detect empty](http://stackoverflow.com/questions/20563166/eloquent-collection-counting-and-detect-empty) – Christophvh May 03 '17 at 07:20

2 Answers2

0

You can always check if your collection is (not) empty and only output the title if it contains something:

@if(!$similar_posts->isEmpty())
    {{ $title }}
@endif

Keep in mind tho that there are several ways to check if something is empty, refer to this post to find out which one suits you the best.

Community
  • 1
  • 1
Adrenaxus
  • 1,593
  • 2
  • 18
  • 34
0

Try this

<div class="title">

  @if( ! $similar_posts->isEmpty() )

    //section title
    <div class="navigation-bar">
        <h3>Title</h3>
    </div>

    @foreach($similar_posts as $related_post)
      //data
    @endforeach

  @endif

</div>
linuxartisan
  • 2,396
  • 3
  • 21
  • 40