沃梦达 / 编程问答 / php问题 / 正文

Laravel 检查集合是否为空

Laravel check if collection is empty(Laravel 检查集合是否为空)

本文介绍了Laravel 检查集合是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Laravel 网络应用中有这个:

I've got this in my 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

我如何检查是否有任何$mentors->intern->employee?

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

当我这样做时:

@if(count($mentors))

它不会检查那个.

推荐答案

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

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

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

这篇关于Laravel 检查集合是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 检查集合是否为空

基础教程推荐