每个注册仅显示一个列表项

时间:2018-07-18 18:46:06

标签: laravel

我有下面的代码来显示用户在会议中的下一个注册:

$nextRegistrations = $user->registrations()
          ->with('participants.registration_type')
          ->whereHas(
              'conference',
              function ($query) {
                  $query->where('end_date', '>', now());
              }
          )->paginate($pageLimit);

然后,如果“ registration_types”表的“ available_certificate”列的值为“ Y”,则在视图中我希望为每个注册显示一个链接“获取证书”。因此,我有下面的代码,但是当一个注册有多个参与者时会出现问题,例如,如果注册有2个与之相关联的参与者,则会出现两个列表项,而不仅仅是一个列表项。你知道怎么解决吗?

<ul class="list-group events-list">
@foreach($nextRegistrations as $nextRegistration)
    @foreach($nextRegistration->participants as $participant)
        @if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
                @if (($participant->registration_type->certificate_available == 'Y')                                                                
                <a href="{{route('conferences.certificateInfo',
                [
                'regID'=> $nextRegistration->id])}}"
                       class="btn btn-primary ml-2">Download certificate</a>
                @endif
            </li>
        @endif
    @endforeach
@endforeach

</ul>

1 个答案:

答案 0 :(得分:0)

它正在显示每个有效参与者的列表项,因为这就是您要它执行的操作。您有一个foreach循环,它表示当参与者有效时,它应该显示一个列表项。如果要在每个注册中显示一个列表项,则需要更改代码。我建议将列表项代码添加到第一个foreach循环中并检查每个参与者,一旦找到有效的参与者,就可以退出foreach循环。看看我的例子:

<ul class="list-group events-list">
    @foreach($nextRegistrations as $nextRegistration)
        @php
            $validRegistration = false;
        @endphp

        @foreach($nextRegistration->participants as $participant)
            @if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
                @if (($participant->registration_type->certificate_available == 'Y')                                                                
                    @php
                        $validRegistration = true;
                    @endphp

                    @break
                @endif
            @endif
        @endforeach

        @if($validRegistration)
            <li><a href="{{ route('conferences.certificateInfo', ['regID' => $nextRegistration->id]) }}" class="btn btn-primary ml-2">Download certificate</a></li>
        @endif
    @endforeach
</ul>