Search code examples
phplaravelhomestead

Laravel - Unable to acces relationship


I have this piece of code $mypost = Post::with('options', 'content')->where('id', '=', $id)->get(); It retrieves the desired post perfectly.

What i want is to be able to access it's relationships, so in this case options and content.

But when I do this: dd($mypost->content); it throws an error.

Property [content] does not exist on this collection instance.

Any help would be greatly appreciated!


Solution

  • By using get, you're getting a Collection of objects, not a single object. You want to use first() to get a single object.

    $mypost = Post::with('options', 'content')->where('id', '=', $id)->first();