Reputation: 581
I'm creating the following view in my controller:
$links = Link::with('cats')->get();
return View::make('home')
->with('links',$links);
This results in the following object:
{"id":1,"user_id":1,"cat_id":3,"link":"http:\/\/www.google.de","active":0,"description":"","created_at":"2014-07-16 19:46:23","updated_at":"2014-07-16 19:46:23",
"cats":[
{"id":1,"cat_name":"Design","created_at":"2014-07-16 00:00:00","updated_at":"2014-07-16 00:00:00","pivot":{"link_id":1,"cat_id":1}}
]},
So my cats are within the link object. But i can't access them.
I've tried this:
{{ $link->cats->cat_name }}
but i get this error:
Undefined property: Illuminate\Database\Eloquent\Collection::$cat_name
What am i doing wrong?
Thanks in advance for any help!
Upvotes: 0
Views: 3998
Reputation: 15750
The answer is in the error message: you're trying to get the $cat_name
property of a collection.
In short, the problem is this: $links->cats
is a collection of cat models. You want to get the name of a specific instance of the cat model, not the collection - the collection doesn't have a name.
Use something like this: $links->cats[0]->cat_name
or iterate over the collection and get each name in turn.
Upvotes: 3