gpt4 book ai didi

php - Laravel Blade 检查空 foreach

转载 作者:IT王子 更新时间:2023-10-29 01:04:33 26 4
gpt4 key购买 nike

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

最佳答案

查看documentation为了最好的结果:

@forelse($status->replies as $reply)
<p>{{ $reply->body }}</p>
@empty
<p>No replies</p>
@endforelse

关于php - Laravel Blade 检查空 foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32652818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com