Laravel 获取关系属性的关系
我想显示链接的注释,并显示用户和用户配置文件之间的注释关系 dd($page->getRelation('links')) 我认为:Laravel 获取关系属性的关系,laravel,Laravel,我想显示链接的注释,并显示用户和用户配置文件之间的注释关系 dd($page->getRelation('links')) 我认为: @foreach($page->getRelation('links') as $link) <div class="link"> <!--show de comments of link (ONLY FOR EXPLANATION)--> @foreach() <div
@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
@foreach($page->getRelation('links')作为$link)
@foreach()
显示注释的DE属性和注释关系的属性(用户、用户配置文件)
@endforeach
@endforeach
请检查下面的文档
雄辩可以在查询父模型时“急切加载”关系。急切加载缓解了N+1查询问题。(…)查询时,可以使用with方法指定应加载哪些关系
例如:
$pages = App\Page::with("link")->get();
foreach ($pages as $page) {
echo $page->links->comments;
}
您还可以加载多个关系
$pages = App\Page::with(['link', 'author'])->get();
最后,您可以嵌套渴望加载关系
o如果要加载嵌套关系,可以使用“点”语法。对于
例如,让我们急切地加载本书的所有作者和所有
作者的个人接触在一个雄辩的声明
首先在控制器函数中加载关系:
$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
@foreach($page->链接为$link)
@foreach($link->comments as$comment)
{{$comment->user}
{{$comment->userProfile}
@endforeach
@endforeach
。通过查看他发布的结果,你也可以看到这一点。
$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