Laravel检查空数组 [英] Laravel Check Empty Array
问题描述
在laravel中检查数组是否为空还是很麻烦.
I still have trouble with checking if an array is empty or not in laravel.
这是我的观点:
@foreach($restaurantmenue as $daily)
@if(empty($daily->articles))
no article
@else
@foreach($daily->articles as $menue)
<a class="card-link" href="#">
<h4 class="title">{{$menue->title}} </h4>
</a>
@endforeach
@endif
@endforeach
{{dd($ daily-> articles}}} 当我查看我的视图时(一个带有文章,另一个不带有文章),我得到以下输出:
{{dd($daily->articles)}} When I check my views (One with an Article and the other without an article) I get this output:
具有现有文章的视图显示: 集合{#228▼ #项:array:1 [▶] }
The View with an existing article shows: Collection {#228 ▼ #items: array:1 [▶] }
没有文章的视图显示: 集合{#227▼ #项目: [] }
And the view without an article shows: Collection {#227 ▼ #items: [] }
我不知道为什么IF STATEMENT中的代码没有执行.不显示无文章"消息.
I have no idea why the code in the IF STATEMENT is not executed. The "No Article" Message is not displayed.
推荐答案
由于它是Laravel集合,因此可以使用 isEmpty()
助手:
Because it's Laravel collection, you can use isEmpty()
helper:
@if($daily->articles->isEmpty())
这篇关于Laravel检查空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!