artisticre's avatar

Relationships and displaying data.

I have two models Users, Posts. Either I am not doing the relationships correctly or there is something I am missing but it is not displaying anything. No error, just empty field on my view

User Model

  public function posts()
   {
       return $this->hasMany('App\Post');
   }

Post Model

public function users(){
        return $this->belongsTo('App\User');
    }

Controller

 public function getSingle($slug)
    {
       $post = Post::where('slug', '=', $slug)->first();
       return view('blog.single')->with('post', $post);

    }

How I Display The Data

{{$post->user['name']}}
0 likes
2 replies
MarianoMoreyra's avatar
Level 25

HI @artisticre

Change the relationship on your Post Model to user instead of users (it's a belongsTo relationship so it should be singular) so it matches the way you are calling it at your view

Please or to participate in this conversation.