Laravel 检查空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39346593/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:25:22  来源:igfitidea点击:

Laravel Check Empty Array

phplaravelis-empty

提问by Mamulasa

I still have trouble with checking if an array is empty or not in laravel.

我仍然无法在 laravel 中检查数组是否为空。

This is my view:

这是我的观点:

@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)}} When I check my views (One with an Article and the other without an article) I get this output:

{{dd($daily->articles)}} 当我检查我的观点(一个有文章,另一个没有文章)时,我得到这个输出:

The View with an existing article shows: Collection {#228 ▼ #items: array:1 [?] }

现有文章的视图显示: Collection {#228 ▼ #items: array:1 [?] }

And the view without an article shows: Collection {#227 ▼ #items: [] }

没有文章的视图显示:收藏{#227 ▼ #items: [] }

I have no idea why the code in the IF STATEMENT is not executed. The "No Article" Message is not displayed.

我不知道为什么不执行 IF STATEMENT 中的代码。不显示“无文章”消息。

回答by Alexey Mezenin

Because it's Laravel collection, you can use isEmpty()helper:

因为是 Laravel 集合,所以可以使用isEmpty()helper:

@if($daily->articles->isEmpty())