Laravel - 无法访问关系

bru*_*one 0 php laravel homestead

我有这段代码$mypost = Post::with('options', 'content')->where('id', '=', $id)->get(); 它完美地检索了所需的帖子。

我想要的是能够访问它的关系,所以在这种情况下options和content.

但是当我这样做时:dd($mypost->content);它会引发错误。

此集合实例上不存在属性 [内容]。

任何帮助将不胜感激!

ayn*_*ber 5

通过使用get,您将获得一个对象集合,而不是单个对象。您想使用它first()来获取单个对象。

$mypost = Post::with('options', 'content')->where('id', '=', $id)->first();
Run Code Online (Sandbox Code Playgroud)