Laravel - 无法访问关系
Laravel - Unable to acces relationship
我有这段代码$mypost = Post::with('options', 'content')->where('id', '=', $id)->get();
它完美地检索了所需的 post。
我想要的是能够访问它的关系,所以在这种情况下 options
和 content
。
但是当我这样做时:dd($mypost->content);
它会抛出一个错误。
Property [content] does not exist on this collection instance.
如有任何帮助,我们将不胜感激!
通过使用 get
,您将获得对象的集合,而不是单个对象。您想使用 first()
获取单个对象。
$mypost = Post::with('options', 'content')->where('id', '=', $id)->first();
我有这段代码$mypost = Post::with('options', 'content')->where('id', '=', $id)->get();
它完美地检索了所需的 post。
我想要的是能够访问它的关系,所以在这种情况下 options
和 content
。
但是当我这样做时:dd($mypost->content);
它会抛出一个错误。
Property [content] does not exist on this collection instance.
如有任何帮助,我们将不胜感激!
通过使用 get
,您将获得对象的集合,而不是单个对象。您想使用 first()
获取单个对象。
$mypost = Post::with('options', 'content')->where('id', '=', $id)->first();