Laravel blade check empty foreach

前端 未结 7 1856
轻奢々
轻奢々 2021-01-31 07:36

I want to check if my foreach is empty so the basic html markup isn\'t displayed with no results inside. I\'m trying to wrap it in an if statement and then if it is empty do not

相关标签:
7条回答
  • 2021-01-31 07:52

    Echoing Data If It Exists

    Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. We can express this in verbose PHP code like so:

    {{ isset($name) ? $name : 'Default' }}
    

    However, instead of writing a ternary statement, Blade provides you with the following convenient short-cut:

    {{ $name or 'Default' }}
    

    In this example, if the $name variable exists, its value will be displayed. However, if it does not exist, the word Default will be displayed.

    From https://laravel.com/docs/5.4/blade#displaying-data

    0 讨论(0)
  • 2021-01-31 07:53

    You should use empty()

    @if (!empty($status->replies)) 
    
    <div class="media-body reply-body">
        @foreach ($status->replies as $reply)
            <p>{{ $reply->body }}</p>
        @endforeach
    </div>
    
    @endif
    

    You can use count, but if the array is larger it takes longer, if you only need to know if its empty, empty is the better one to use.

    0 讨论(0)
  • 2021-01-31 07:53

    Using following code, one can first check variable is set or not using @isset of laravel directive and then check that array is blank or not using @unless of laravel directive

    @if(@isset($names))
        @unless($names)
            Array has no value
        @else
            Array has value
    
            @foreach($names as $name)
                {{$name}}
            @endforeach
    
        @endunless
    @else
        Not defined
    @endif
    
    0 讨论(0)
  • 2021-01-31 08:04

    I think you are trying to check whether the array is empty or not.You can do like this :

    @if(!$result->isEmpty())
         // $result is not empty
    @else
        // $result is empty
    @endif
    

    Reference isEmpty()

    0 讨论(0)
  • 2021-01-31 08:05

    Check the documentation for the best result:

    @forelse($status->replies as $reply)
        <p>{{ $reply->body }}</p>
    @empty
        <p>No replies</p>
    @endforelse
    
    0 讨论(0)
  • 2021-01-31 08:05

    This is my best solution if I understood the question well:

    Use of $object->first() method to run the code inside if statement once, that is when on the first loop. The same concept is true with $object->last().

        @if($object->first())
            <div class="panel user-list">
              <table id="myCustomTable" class="table table-hover">
                  <thead>
                      <tr>
                         <th class="col-email">Email</th>
                      </tr>
                  </thead>
                  <tbody>
        @endif
    
        @foreach ($object as $data)
            <tr class="gradeX">
               <td class="col-name"><strong>{{ $data->email }}</strong></td>
            </tr>
        @endforeach
    
        @if($object->last())
                    </tbody>
                </table>
            </div>
        @endif
    
    0 讨论(0)
提交回复
热议问题