获取关系属性的关系 [英] Get relation of relation attributes

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

问题描述

我想显示链接的注释并显示用户和用户配置文件之间的注释关系...

I want to show the comments of link and show de relations of comments that is user and userprofile...

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

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

Collection {#318 ▼
  #items: array:2 [▼
    0 => Link {#317 ▼
      #relations: array:2 [▼
        "comments" => Collection {#327 ▼
              #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 ▶}
                "userProfile" => UserProfile {#326 ▶}
              ]
            }
          ]
        }

我认为:

        @foreach($page->getRelation('links') as $link)
        <div class="link">

    <!--show de comments of link (ONLY FOR EXPLANATION)-->
@foreach()
        <div class="comments">
        SHOW DE ATTRIBUTE OF COMMENT AND ATTRIBUTE OF RELATIONS OF COMMENT (user, userprofile)
        </div>
@endforeach

        </div>

        @endforeach

推荐答案

首先在控制器函数中加载关系:

first load the relationships in the controller function:

$page = Page::where($allYourConditions)->with(['links' => 
function($query){
  $query->with(['comments' => function($query){
    $query->with(['user', 'userProfile']);
  }]);
}])->get();

然后在视图文件中:

@foreach($page->links as $link)
  <div class="link">
  @foreach($link->comments as $comment)
    <div class="comment">
      <div class="comment-user">{{$comment->user}}</div>
      <div class="comment-user-profile">{{$comment->userProfile}}</div>
    </div>
  @endforeach
@endforeach

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

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