Consider this relation:
// in Post model
public function user()
{
return $this->belongsTo('App\Models\User');
}
I think $post->user->name
should be enough to get the name of post's owner. However $post->user
return the user id instead of the user model. So I have to use:
$post->user()->first()->name;
Shouldn't $post->user
return the post's owner?
Does a user belong to a post or does a post belong to a user?
From my perspective, a user has many posts, and a post belongs to a user. So I think you have messed up your relations.
class Post extends Model
{
public function user()
{
return $this->belongsTo('User');
}
}
class User extends Model
{
public function posts()
{
return $this->hasMany('Post');
}
}
Then, once you have a Post, you will be able to access it's user.
$userName = Post::find(1)->user->name;