I have this code to show the next registrations of a user in a conference:
$nextRegistrations = $user->registrations()
->with('participants.registration_type')
->whereHas(
'conference',
function ($query) {
$query->where('end_date', '>', now());
}
)->paginate($pageLimit);
Then, in the view I want to show for each registration a link "Get certificate" if the column "available_certificate" of the "registration_types" table has the value "Y". But its not working with "@if ($nextRegistration->participants->contains('certificate_available', 'Y'))". Do you know why? The the " {{dump($nextRegistration->participants->contains('certificate_available', 'Y'))}}
" shows "false
" for the 3 list items (3 registrations).
<ul class="list-group">
@foreach($nextRegistrations as $nextRegistration)
@foreach($nextRegistration->participants as $participant)
@if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
@if ($nextRegistration->participants->contains('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>
Db structure:
conferences table:
id name
1 conference test 1
registrations table:
id conference_id user_that_did_registration
1 1 2
2 1 2
3 1 2
participants table:
id registration_id registration_type_id name
1 1 1 Jake
2 2 1 John
3 3 1 Paul
4 3 2 Peter
registration_types table:
id name conference_id certificate_id certificate_available
1 r1 1 1 Y
2 r2 1 2 Y
certificate table:
id content
1 <p>cert 1</p>
2 <p>cert 2</p>
I think you need to filter and then check the count, see our comments above:
$results = $nextRegistration->participants->filter(function ($participant) {
return $participant->registration_type->contains('certificate_available', 'Y');
});
if (count($results) > 0) {
// Do something
}