我想检查我的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