php Laravel 刀片检查空 foreach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32652818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel blade check empty foreach
提问by Sam
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 nothing else loop the 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
回答by cre8
Check the documentationfor the best result:
检查文档以获得最佳结果:
@forelse($status->replies as $reply)
<p>{{ $reply->body }}</p>
@empty
<p>No replies</p>
@endforelse
回答by Kiran Subedi
回答by iain
You should use empty()
你应该使用 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.
您可以使用 count,但如果数组较大则需要更长的时间,如果您只需要知道它是否为空,则使用 empty 更好。
回答by William Turrell
It's an array, so ==== ''
won't work (the === means it has to be an empty string.)
它是一个数组,所以==== ''
不起作用(=== 意味着它必须是一个空字符串。)
Use count()to identify the array has any elements (count returns a number, 1 or greater will evaluate to true, 0 = false.)
使用count()来识别数组是否有任何元素(count 返回一个数字,1 或更大的将评估为真,0 = 假。)
@if (count($status->replies) > 0)
// your HTML + foreach loop
@endif
回答by Nabeel Shah
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.
回显数据(如果存在)
有时您可能希望回显一个变量,但您不确定该变量是否已设置。我们可以像这样用冗长的 PHP 代码来表达:
{{ isset($name) ? $name : 'Default' }}
但是,Blade 为您提供了以下方便的快捷方式,而不是编写三元语句:
{{ $name or 'Default' }}
在此示例中,如果 $name 变量存在,则将显示其值。但是,如果它不存在,则会显示“默认”一词。
回答by Mugove Junior Machaka
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()
.
使用$object->first()
方法运行if
一次语句里面的代码,也就是在第一次循环时。同样的概念也适用于$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
回答by user3216114
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
使用以下代码,可以首先使用laravel指令的@isset检查变量是否设置,然后使用laravel指令的@unless检查数组是否为空
@if(@isset($names))
@unless($names)
Array has no value
@else
Array has value
@foreach($names as $name)
{{$name}}
@endforeach
@endunless
@else
Not defined
@endif