Do you know why the " @if ($nextRegistration->participants->registration_type->contains('certificate_available', 'Y')) [
" shows "Property [registrationType] does not exist on this collection instance
"?
I want to show for each registration a link "Get certificate" if the column "certificate_available" of the "registration_types" table has the value "Y".
<ul class="list-group">
@foreach($nextRegistrations as $nextRegistration)
@if(!empty($nextRegistration->conference) || !empty($nextRegistration->conference->start_date))
@if ($nextRegistration->participants->registration_type->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
</ul>
The participant model has the registration_type():
class Participant extends Model
{
public function registration(){
return $this->belongsTo('App\Registration');
}
public function registration_type(){
return $this->belongsTo('App\RegistrationType');
}
}
To get the $nextRegistrations the code is:
$nextRegistrations = $user->registrations()
->with('participants.registration_type')
->whereHas(
'conference',
function ($query) {
$query->where('end_date', '>', now());
}
)->paginate($pageLimit);
And $nextRegistrations shows like:
LengthAwarePaginator {#336 ▼
#total: 3
#lastPage: 1
#items: Collection {#320 ▼
#items: array:3 [▼
0 => Registration {#308 ▼
...
#relations: array:1 [▼
"participants" => Collection {#327 ▼
#items: array:1 [▼
0 => Participant {#332 ▼
...
#relations: array:1 [▼
"registration_type" => RegistrationType {#337 ▼
#fillable: array:7 [▶]
....
#attributes: array:10 [▼
"id" => 1
"name" => "general"
"description" => "desc general"
"conference_id" => 1
"certificate_id" => 1
"certificate_available" => "Y"
]
...
}
]
...
}
]
}
]
...
}
1 => Registration {#321 ▶}
2 => Registration {#317 ▶}
]
}
....
}
How do you retrieve the data from the database. You may need to use the with method:
$data = Participant::with('registration_type')->get()
or in the view use "registration_type" as a method, not as a property:
$nextRegistration->participants->registration_type()
You can find more about the belongs to relationship here: https://laravel.com/docs/5.6/eloquent-relationships#one-to-many-inverse