本文介紹了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 檢查集合是否為空的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!