php Laravel 检查集合是否为空

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

Laravel check if collection is empty

phplaravellaravel-5.2

提问by Jamie

I've got this in my Laravel webapp:

我在我的 Laravel webapp 中有这个:

@foreach($mentors as $mentor)
    @foreach($mentor->intern as $intern)
        <tr class="table-row-link" data-href="/werknemer/{!! $intern->employee->EmployeeId !!}">
            <td>{{ $intern->employee->FirstName }}</td>
            <td>{{  $intern->employee->LastName }}</td>
        </tr>
    @endforeach
@endforeach

How could I check if there are any $mentors->intern->employee?

我怎么能检查是否有$mentors->intern->employee

When I do :

当我做 :

@if(count($mentors))

It does not check for that.

它不检查。

采纳答案by otezz

You can always count the collection. For example $mentor->intern->count()will return how many intern does a mentor have.

您可以随时计算集合。例如$mentor->intern->count()将返回导师有多少实习生。

https://laravel.com/docs/5.2/collections#method-count

https://laravel.com/docs/5.2/collections#method-count

In your code you can do something like this

在您的代码中,您可以执行以下操作

foreach($mentors as $mentor)
    @if($mentor->intern->count() > 0)
    @foreach($mentor->intern as $intern)
        <tr class="table-row-link" data-href="/werknemer/{!! $intern->employee->EmployeeId !!}">
            <td>{{ $intern->employee->FirstName }}</td>
            <td>{{  $intern->employee->LastName }}</td>
        </tr>
    @endforeach
    @else
        Mentor don't have any intern
    @endif
@endforeach

回答by Drudge Rajen

To determine if there are any results you can do any of the following:

要确定是否有任何结果,您可以执行以下任一操作:

if ($mentor->first()) { } 
if (!$mentor->isEmpty()) { }
if ($mentor->count()) { }
if (count($mentor)) { }
if ($mentor->isNotEmpty()) { }

Notes / References

注释/参考

->first()

->first()

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_first

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_first

isEmpty()https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_isEmpty

isEmpty()https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_isEmpty

->count()

->count()

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_count

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_count

count($mentors)works because the Collection implements Countable and an internal count() method:

count($mentors)之所以有效,是因为 Collection 实现了 Countable 和一个内部 count() 方法:

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_count

https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_count

isNotEmpty()

isNotEmpty()

https://laravel.com/docs/5.7/collections#method-isnotempty

https://laravel.com/docs/5.7/collections#method-isnotempty

So what you can do is :

所以你可以做的是:

if (!$mentors->intern->employee->isEmpty()) { }

回答by Mohamed Salem Lamiri

Starting from Laravel 5.3you can simply use :

Laravel 5.3开始,您可以简单地使用:

if ($mentor->isNotEmpty()) {
//do something.
}

Documentation https://laravel.com/docs/5.5/collections#method-isnotempty

文档https://laravel.com/docs/5.5/collections#method-isnotempty

回答by Csongor Halmai

This is the fastest way:

这是最快的方法:

if ($coll->isEmpty()) {...}

Other solutions like countdo a bit more than you need which costs slightly more time.

其他解决方案count比您需要的要多一些,这会花费更多的时间。

Plus, the isEmpty()name quite precisely describes what you want to check there so your code will be more readable.

另外,该isEmpty()名称非常准确地描述了您想要检查的内容,因此您的代码将更具可读性。

回答by d.raev

From php7you can use Null Coalesce Opperator:

php7您可以使用空合并Opperator

$employee = $mentors->intern ?? $mentors->intern->employee

This will return Nullor the employee.

这将返回Null或员工。

回答by Marcello Patto

I prefer

我更喜欢

(!$mentor)

(!$mentor)

Is more effective and accurate

更有效和准确