控制器:$page = Page::where('friendly_url', $id) ->select('id', 'photo', 'friendly_url', 'name', 'description', 'followers', 'links', 'tag_id', 'links_clicks') ->with('ptag', 'links', 'links.comments', 'links.tag') ->with(['links.comments.user' => function($query) { $query->select('id', 'name', 'lastname'); }])->with(['links.comments.userProfile' => function($query) { $query->select('id', 'photo'); }])->first(); dd($ page-> getRelation('links')Collection {#301 ▼ #items: array:2 [▼ 0 => Link {#307 ▼ #relations: array:2 [▼ "comments" => Collection {#316 ▼ #items: array:1 [▶] "tag" => Tag {#322 ▶} ] dd($ page-> getRelation('links')-> getRelation('comments'))BadMethodCallExceptionMethod getRelation does not exist. dd($ page-> getRelation('links')->评论)ExceptionProperty [comments] does not exist on this collection instance.我想显示comment和comment.user的属性,我该怎么做?foreach($page->getRelation('links')->pluck('comments') as $comment) { dd($comment); }Collection {#327 ▼ #items: array:1 [▼ 0 => Comment {#325 ▼ #attributes: array:5 [▼ "id" => 3 "content" => "teste" "link_id" => 1 "user_id" => 1 "created_at" => "2018-01-11 00:47:32" ] #relations: array:2 [▼ "user" => User {#330 ▼ #attributes: array:3 [▼ "id" => 1 "name" => "Halysson" "lastname" => "Teves dos Santos" ] } "userProfile" => UserProfile {#326 ▶} ]解决方案如果您想要页面的链接:$page->links如果要页面的所有注释:$page->links->pluck('comments')就这样:)I'm trying to get the comments of the links with laravel relationship, the comments of links return normally, but I don't know how to get it in view or dd(), how can I do it?Controller:$page = Page::where('friendly_url', $id) ->select('id', 'photo', 'friendly_url', 'name', 'description', 'followers', 'links', 'tag_id', 'links_clicks') ->with('ptag', 'links', 'links.comments', 'links.tag') ->with(['links.comments.user' => function($query) { $query->select('id', 'name', 'lastname'); }])->with(['links.comments.userProfile' => function($query) { $query->select('id', 'photo'); }])->first();dd($page->getRelation('links')Collection {#301 ▼ #items: array:2 [▼ 0 => Link {#307 ▼ #relations: array:2 [▼ "comments" => Collection {#316 ▼ #items: array:1 [▶] "tag" => Tag {#322 ▶} ]dd($page->getRelation('links')->getRelation('comments'))BadMethodCallExceptionMethod getRelation does not exist.dd($page->getRelation('links')->comments)ExceptionProperty [comments] does not exist on this collection instance.EDIT:I want to show the attributes of comment and comment.user, how I can do it?foreach($page->getRelation('links')->pluck('comments') as $comment) { dd($comment); }Collection {#327 ▼ #items: array:1 [▼ 0 => Comment {#325 ▼ #attributes: array:5 [▼ "id" => 3 "content" => "teste" "link_id" => 1 "user_id" => 1 "created_at" => "2018-01-11 00:47:32" ] #relations: array:2 [▼ "user" => User {#330 ▼ #attributes: array:3 [▼ "id" => 1 "name" => "Halysson" "lastname" => "Teves dos Santos" ] } "userProfile" => UserProfile {#326 ▶} ] 解决方案 If you want the links of a page : $page->linksif you want all the comments of a page : $page->links->pluck('comments')that's it :) 这篇关于获取关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 06:01