If you have () you point to the method not the relation. It should be
dd($activity->contact->id)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hey All - I'm running Laravel 6.2 and am getting an error trying to get any attribute of a model through a "belongsTo" relationship. I'm using a "hasmanythrough" relationship to get activities from a user model - maybe that has something to do with it?
Also I noticed if I dump and die dd($activity->contact()) I can see the contact model but when I try dd($activity->contact()->id) it will fail. Very perplexing!
Controller:
public function index() {
$user = Auth::user();
$activities = $user->activities();
return view( 'activity.index', [ 'activities' => $activities ] );
}
Activity Model
public function contact() {
return $this->belongsTo( 'App\Contact' )->first();
}
Blade template
@if ( $activities->count() )
@foreach ( $activities as $activity )
<div class="contact route" data-url="/outreach/list?q={{ $activity->id }}">
@php dump($activity->contact()->id) @endphp
</div>
@endforeach
@endif
Also be sure that all activities have a contact
Or do
$activity->contact->id ?? ''
Please or to participate in this conversation.