获取关系 [英] Get relation of relation

查看:76
本文介绍了获取关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取具有laravel关系的链接的注释,链接的注释正常返回,但是我不知道如何在视图或dd()中获取它,我该怎么做?

控制器:

$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'))

BadMethodCallException
Method getRelation does not exist.

dd($ page-> getRelation('links')->评论)

Exception
Property [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'))

BadMethodCallException
Method getRelation does not exist.

dd($page->getRelation('links')->comments)

Exception
Property [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->links

if you want all the comments of a page :

$page->links->pluck('comments')

that's it :)

这篇关于获取关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆