我的 Laravel 网络应用程序中有这个:
@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
?
当我做 :
@if(count($mentors))
它不检查这一点。
要确定是否有任何结果,您可以执行以下任一操作:
if ($mentor->first()) { }
if (!$mentor->isEmpty()) { }
if ($mentor->count()) { }
if (count($mentor)) { }
if ($mentor->isNotEmpty()) { }
注释/参考文献
->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 https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Collection.html#method_isEmpty
->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)
之所以有效,是因为 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()
https://laravel.com/docs/5.7/collections#method-isnotempty https://laravel.com/docs/5.7/collections#method-isnotempty
所以你可以做的是:
if (!$mentors->intern->employee->isEmpty()) { }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)