Laravel blade check empty foreach

前端 未结 7 1896
轻奢々
轻奢々 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 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())
            
    @endif @foreach ($object as $data) @endforeach @if($object->last())
    {{ $data->email }}
    @endif

提交回复
热议问题