2
votes

I have 3 Models "Category", "Post", and "User". A Category has a hasMany relationship with Post. And a Post has a belongsTo relationship with User.

I have a Category object $cat1 and i can access its posts (and the user_id) in my view, but i can't access more user data (line name)

@foreach ($cat1->posts as $post)
    {{ $post->title }}
    {{ $post->user()->name }}
@endforeach

This throws an error

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name

1

1 Answers

4
votes

You can access it like this:

{{ $post->user->name }}

When you call the function, it's to query the relationship.