php – Laravel刀片检查空的foreach

前端之家收集整理的这篇文章主要介绍了php – Laravel刀片检查空的foreach前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想检查我的foreach是否空,所以基本的html标记不显示,没有内部的结果.我试图将它包装在一个if语句中,然后如果它是空的,不用再循环foreach.
@if ($status->replies === '')

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

@if (!(empty($status->replies))
<div class="media-body reply-body">
    @foreach ($status->replies as $reply)
        <div class="media">
            <a class="pull-left" href="{{ route('profile.index',['username' => $reply->user->username]) }}">
                <img class="media-object" alt="{{ $reply->user->getNameOrUsername() }}" src="{{ $reply->user->getAvatarUrl() }}">
            </a>
            <div class="media-body">
                <h5 class="media-heading"><a href="{{ route('profile.index',['username' => $reply->user->username]) }}">{{ $reply->user->getNameOrUsername() }}</a></h5>
                <p>{{ $reply->body }}</p>
                <ul class="list-inline list-replies">
                    <li>
                        <a href="{{ route('status.like',['statusId' => $reply->id]) }}"><i class="fa fa-thumbs-up"></i></a>
                    {{ $reply->likes->count() }} {{ str_plural('like',$reply->likes->count()) }}</li>
                    <li>{{ $reply->created_at->diffForHumans() }}</li>
                </ul>
            </div>
            <hr>
        </div>
    @endforeach
</div>
@endif
检查文档以获得最佳效果
@forelse($status->replies as $reply)
    <p>{{ $reply->body }}</p>
@empty
    <p>No replies</p>
@endforelse

Documentation.

原文链接:https://www.f2er.com/laravel/132661.html

猜你在找的Laravel相关文章