Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stretch0's avatar

How to access relationship attributes

I am trying to echo the title of the post in the replies section of a forum I am building.

The code in the view is essentially

<h4 class="pull-left">Post Name {{dd($replies)}}</h4>>

If I do a dd of the replies array I get:

      Collection {#188 ▼
#items: array:1 [▼
  0 => Reply {#184 ▼
    #connection: null
    #table: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:7 [▶]
    #original: array:7 [▶]
    #relations: array:2 [▼
      "user" => User {#189 ▶}
      "post" => Post {#190 ▼
        #connection: null
        #table: null
        #primaryKey: "id"
        #perPage: 15
        +incrementing: true
        +timestamps: true
        #attributes: array:8 [▶]
        #original: array:8 [▶]
        #relations: []
        #hidden: []
        #visible: []
        #appends: []
        #fillable: []
        #guarded: array:1 [▶]
        #dates: []
        #casts: []
        #touches: []
        #observables: []
        #with: []
        #morphClass: null
        +exists: true
      }
    ]
    #hidden: []
    #visible: []
    #appends: []
    #fillable: []
    #guarded: array:1 [▶]
    #dates: []
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: true
  }
]
 }

My controller is as follows: (you will see I have hard coded the parent post id as 1 and need to know how I can access the appropriate ID from the reply_id column in my db)

    public function index($id)
    {
        //show replies to post
        $replies = Reply::with('user')->where('post_id', $id)->with('post')->where('id', 1)->get();

        return view('replies')->with('replies', $replies);
    }
0 likes
6 replies
RachidLaasri's avatar
public function index($id)
{
        $post = Post::with('user', 'replies')->where('post_id', $id)->get();

        return view('replies')->with('post', $post);
}

echoing data :

Post Title : {{ $post->title }}
Created by : {{ $post->user->username }}
Replies : 
@foreach($post->replies as $reply)
  {{ $reply->comment }}
@endforeach
stretch0's avatar

Thanks that fixed the controller but I still can't echo out the replies.

Controller is now as follows:

    public function index($id)
    {
        //show replies to post
        $post = Post::with('user', 'reply')->where('id', $id)->get();

        return view('replies')->with('post', $post);
    }


    {{ $post->name }}

returns error "Undefined property:"

It is really strange because if I echo the post variable via

{{$post}}   

I can see it returns array

 {"id":1,"name":"Excellent service","content":"I have been receiving excellent service from my carrier as of    lately","topic_id":"1","created_at":"2015-05-23 13:54:57","updated_at":null,"deleted_at":null,"user_id":null,"user":null,"reply":   [{"id":1,"content":"That's awesome","created_at":"2015-05-24    10:22:33","updated_at":null,"deleted_at":null,"user_id":1,"post_id":1}]}
jekinney's avatar

@stretch0 When I have build Forums, blogs with comments and replies and discussin boards I use eager loading

Assume you have a category with topics and you can reply to topics. I set my user relationship as a Author for readability and it isn't a good idea for security and SEO to pass the ID in the URL, just FYI. On a topics view I run a query like this:

$post = Post::with('replies', 'replies.author', 'author')->where('slug', $slug)->first();

The replies.author is eager loading the replies author relationship (nested relationships). View then

{{ $post->title }}
{{ $post->body }}
{{ $post->author->name }}
@foreach($post->replies as $reply)
    {{ $reply->author->name }}
    {{ $reply->body }}
@endforeach

Obviously need some HTML, but your main issue is the get() on your query instead of first (or firstOrFail()) if you do pass the ID you can also use the find($id) too. When you use the get() you are expected to loop through the results and the find() and first() will only get one result so you don't need to loop through it.

pmall's avatar

use :

$post = Post::with('user', 'reply')->findOrfail($id);

Also eager loading here is useless as you are retrieving only one record. It will execute three queries with or without eager loading (one for the post, one for the user, one for the replies). It would be useful only to get the whole post and relationship array as json for example.

RachidLaasri's avatar

The code is fine, you just don't have a title field, it's "name" instead.

stretch0's avatar

@RachidLaasri My bad, that was a typo and wasn't what I had in my code at the time of testing. I have sinced edited post.

Please or to participate in this conversation.