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
相关文章