I'm trying to get the profile that relates to the given user on my website.
The relation is setup correctly (I can do this: Auth::user() -> profile
to access profile data. But when I try to do this: User::findOrFail(1) -> profile
it gives me an error saying that it's not an object (and the value of profile is equal to null
. Why is that? And yes the user with the ID of 1
does exist.
In my User
model I've setup this:
public function profile() {
return $this -> hasOne('App\Profile', 'user_id');
}
And in my Profile
model I've done this:
public function user() {
return $this -> belongsTo('App\User', 'user_id');
}
I just saw this problem when grabbing X users and going through them in a foreach loop as seen below.
<div class="thumbs-wrapper text-center">
@foreach ($last_online as $user)
<div class="thumb @if(\App\User::isOnline($user -> id)) online @else offline @endif">
<a href="/profile/view/{{ $user -> id }}">
<img src="{{ asset($user -> profile -> picture) }}">
</a>
</div>
@endforeach
</div>
What could be wrong? Thanks in advance.