Laravel 4.1:如何分享雄辩的渴望关系? [英] Laravel 4.1: How to paginate eloquent eager relationship?

查看:85
本文介绍了Laravel 4.1:如何分享雄辩的渴望关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关旧的L3渴望加载分页的问题,没有使用雄辩。但是,我想用雄辩的方式来获得与分页的热切关系。

There was question about old L3 eager loaded paginations, not using eloquent. But i want to use eloquent to get eager loaded relationship with pagination.

主要模式:主题帖子
所以一个主题有很多帖子。我得到所有的数据与这个功能:

Main model: Topic that has one to many relationship with Posts, So that one Topic has many Posts. I get all data with this function:

public function findById($id)
{
    return $this->topic->with('posts', 'posts.user', 'posts.user.profile')
                       ->find($id);
}

稍后我使循环显示所有结果,但它们没有分页:

And later I make loop to display all results, but they are not paginated:

@foreach($topic->posts as $post)
... unpaginated content ...
@endforeach

所以,我可以做一个解决方法,并单独选择所有具有$ id的帖子的主题和使用 - > paginate()而不是 - > get(),并将分页$ pot,

So, i could make a workaround and select separately all posts that has $id of topic and use ->paginate() instead of ->get() and would got paginated $pots,


  1. 但是有可能使用分页的热心关系帖子

  1. but is there possibility to use eager loaded relationship posts that is paginated ? And how can it be done ?


推荐答案

澄清一些东西:分页热切的关系是有点误解。渴望加载的要点是尽可能少的查询检索所有关系。如果要检索10个主题,所有这些主题都有35个帖子,则只需要两个查询。甜心!

To clarify something: paginating eager-loaded relationships is somewhat of a misconception. The point of eager loading is to retrieve all relationships in as few queries as you can. If you want to retrieve 10 topics, all of which have 35 posts, you will only need two queries. Sweet!

这样说,分页一个渴望加载的关系不会奏效。想要加载的时候考虑两种情况:

That said, paginating an eager-loaded relationship is not going to work. Consider two scenarios when eager loading happens:


  1. 您想要检索和列出主题,也许列出每个话题。大!渴望加载是完美的。现在,你不想在这样的页面上分页热门的帖子,所以没关系。

  1. You want to retrieve and list topics, and maybe list the first five posts for each topic. Great! Eager loading is perfect. Now, you wouldn't want to paginate the eager-loaded posts on a page like this, so it doesn't matter.

你加载一个主题,并且您想分页该主题的帖子。大!比较容易做但是,如果您已经热切地加载了属于此主题的所有帖子,那么您只是可能检索了大量不需要的额外资源。因此,急切的加载其实是伤害你的。

You load a single topic, and you want to paginate the posts for that topic. Great! Relatively easy to do. However, if you've already eager-loaded all posts belonging to this topic, you've just potentially retrieved a lot of extra resources that you don't need. Therefore eager loading is actually hurting you.

>,有两个潜在的解决方案:

That said, there are two potential solutions:

选项1:创建一个自定义访问器,用于分页雄辩的关系。

Option 1: Create a custom accessor that paginates the Eloquent relationship.

/**
 * Paginated posts accessor. Access via $topic->posts_paginated
 * 
 * @return \Illuminate\Pagination\Paginator
 */
public function getPostsPaginatedAttribute()
{
    return $this->posts()->paginate(10);
}

优点:分页非常简单;不妨碍正常的职位关系。

缺点:加载帖子不会影响此访问者;运行它将在热切加载的查询之上创建两个附加查询。

Pros: Paginates very easily; does not interfere with normal posts relationship.
Cons: Eager loading posts will not affect this accessor; running it will create two additional queries on top of the eager loaded query.

选项2:分页帖子由热切关系返回的集合。

Option 2: Paginate the posts Collection returned by the eager-loaded relationship.

/**
 * Paginated posts accessor. Access via $topic->posts_paginated
 * 
 * @return \Illuminate\Pagination\Paginator
 */
public function getPostsPaginatedAttribute()
{
    $posts = $this->posts;

    // Note that you will need to slice the correct array elements yourself.
    // The paginator class will not do that for you.

    return \Paginator::make($posts, $posts->count(), 10);
}

优点:使用渴望加载的关系;创建没有其他查询。

缺点:不管当前页面(慢),都必须检索所有元素;必须手动构建当前页面元素。

Pros: Uses the eager-loaded relationship; creates no additional queries.
Cons: Must retrieve ALL elements regardless of current page (slow); have to build the current-page elements manually.

这篇关于Laravel 4.1:如何分享雄辩的渴望关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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