Laravel check if collection is empty

前端 未结 7 1145
清酒与你
清酒与你 2020-12-08 00:10

I\'ve got this in my Laravel webapp:

@foreach($mentors as $mentor)
    @foreach($mentor->intern as $intern)
        

        
7条回答
  •  时光说笑
    2020-12-08 00:58

    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)
            
                {{ $intern->employee->FirstName }}
                {{  $intern->employee->LastName }}
            
        @endforeach
        @else
            Mentor don't have any intern
        @endif
    @endforeach
    

提交回复
热议问题