Laravel 5-查找模型的分页页面 [英] Laravel 5 - Finding the pagination page for a model

查看:108
本文介绍了Laravel 5-查找模型的分页页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个基础论坛(灵感来自 laracasts.com/discuss ).当用户发布对主题的回复时:

I am working on building a basic forum (inspired by laracasts.com/discuss). When a user posts a reply to a thread:

  • 我想用他们的回复锚点将他们引导到分页回复列表的末尾.
  • 我还想在用户编辑其答复之一时使用户返回到正确的页面.
  • I'd like to direct them to the end of the list of paginated replies with their reply's anchor (same behavior as Laracasts).
  • I'd also like to return the user to the correct page when they edit one of their replies.

我如何确定新的答复将发布在(?page=x)的哪一页上,并且在答复被编辑后如何返回正确的页面?或者,在主要帖子列表中,最新回复位于哪个页面上?

How can I figure out which page a new reply will be posted on (?page=x) and how can I return to the correct page after a reply has been edited? Or, from the main post listing, which page the latest reply is on?

这是我当前的ForumPost模型(减去一些无关的内容)-

Here is my current ForumPost model (minus a few unrelated things) -

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * Class ForumPost
 *
 * Forum Posts table
 *
 * @package App
 */
class ForumPost extends Model {
    /**
     * Post has many Replies
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function replies()
    {
        return $this->hasMany('App\ForumReply');
    }

    /**
     * Get the latest reply for a post
     * @return null
     */
    public function latestReply()
    {
        return $this->replies()->orderBy('created_at', 'desc')->first();
    }

}

更新

看看这个,让我知道您的想法.它的工作方式有点怪异,但它会针对给定的回复ID返回正确的页面,这只是一种方法:

Take a look at this and let me know what you think. It's a bit weird in how it works but it's returning the correct page for a given reply ID and it's just one method:

public function getReplyPage($replyId = null, $paginate = 2)
    {
        $id = $replyId ? $replyId : $this->latestReply()->id;
        $count = $this->replies()->where('id', '<', $id)->count();

        $page = 1; // Starting with one page

        // Counter - when we reach the number provided in $paginate, we start a new page
        $offset = 0;

        for ($i = 0; $i < $count; $i++) {

            $offset++;
            if ($offset == $paginate) {
                $page++;
                $offset = 0;
            }
        }


        return $page;
    }

推荐答案

从根本上讲,您正在使用两个值:首先,答复的索引与帖子的所有答复有关,其次是答复的数量在页面上.

Fundamentally you are working with two values: first, what the index of a reply is in relation to all the replies of a post, and second the number of replies in on a page.

例如,您可能收到ID为301的回复.但是,这是特定帖子的第21条回复.您需要某种方式来确定这是第21条回复.这实际上是相对简单的:您只需计算与该帖子相关的回复数量,但ID较小.

For example, you might have a reply with an id of 301. However, it is the 21st reply on a specific post. You need to some way to figure out that it is the 21st reply. This is actually relatively simple: you just count how many replies are associated with that post but have smaller ids.

//get the number of replies before the one you're looking for
public function getReplyIndex($replyId)
{
    $count = $this->replies()->where('id', '<', $replyId)->count();
    return $count;
}

该方法应该返回您正在寻找的答复的索引-当然,假设您的答复使用的是自动递增ID.

That method should return the index of the reply you are looking for based- assuming, of course, that your replies are using auto-increment ids.

难题的第二部分是弄清楚您需要哪一页.这是通过整数除法完成的.基本上,您通常只将数字相除,而不使用余数.如果您正在查看第21条答复,并且您对某个页面有10条答复,那么您应该知道它应该在第三页上(第1页:1-10,第2页:11-20,第3页:21-30).这意味着您需要将您的答复索引除以每页的答复数,然后加1.这将给我们21/10 + 1,使用整数除法将给我们3.是的!

The second piece of the puzzle is figuring out which page you need. This is done using integer division. Basically you just divide the number normally, but don't use the remainder. If you are looking at the 21st reply, and you have 10 replies to a page, you know it should be on the third page (page 1: 1-10, page 2: 11-20, page 3: 21-30). This means you need to integer divide your reply index by your replies-per-page, then add 1. This will give us 21/10+1, which, using integer division, gives us 3. Yay!

//divides where we are by the number of replies on a page and adds 1
public function getPageNumber($index, $repliesPerPage)
{
    $pageNumber = (int) ($index/$repliesPerPage+1);
    return $pageNumber;
}

好的,所以现在您只需要拉出该页面即可.这仅需要一种方法,您可以在其中指定所需的页码以及对一个页面的回复数.然后,该方法可以计算偏移量和限制,并检索所需的记录.

Alright, so now you just need to pull that page. This simply requires a method where you specify what page number you need, and how many replies to a page there are. That method can then calculate the offset and the limit, and retrieve the records you need.

public function getPageOfReplies($pageNumber, $repliesPerPage)
{
    $pageOfReplies = $this->replies()->offset($pageNumber*$repliesPerPage)->limit($repliesPerPage)->get();
    return $pageOfReplies;
}

尽管如此,我们可以构建一种方法来获取最终答复的索引.

For good measure, though, we can build a method to get the index of the final reply.

public function getLastReplyIndex()
{
    $count = $this->replies()->count();
    return $count;
}

太好了!现在,我们有了所需的所有构件.我们可以构建一些简单的方法,使用我们的通用方法轻松地检索所需的数据.

Great! Now we have all the building blocks we need. We can build some simple methods that use our more general-purpose ones to easily retrieve the data we need.

让我们从一种方法开始,该方法获取单个答复所在的整个答复页面(可以随意更改名称(我也假设每页有10个答复)):

Let's start with a method that gets the entire page of replies on which a single reply resides (feel free to change the names (also I'm assuming there are 10 replies per page)):

public function getPageThatReplyIsOn($replyId)
{
    $repliesPerPage = 10;
    $index = $this->getReplyIndex($replyId);
    $pageNumber = $this->getPageNumber($index, $repliesPerPage);
    return $this->getPageOfReplies($pageNumber, $repliesPerPage);
}

从长远来看,我们可以采用一种方法来获取最终答复的页面.

For good measure, we can make a method that gets the page of final replies.

public function getFinalReplyPage()
{
    $repliesPerPage = 10;
    $index = $this->getLastReplyIndex();
    $pageNumber = $this->getPageNumber($index, $repliesPerPage);
    return $this->getPageOfReplies($pageNumber, $repliesPerPage);
}

您可以构建各种其他方法来使用我们的构建块方法并跳转页面,在回复之前或之后获取页面,等等.

You could build a variety of other methods to use our building block methods and jump around pages, get the pages after or before a reply, etc.

一些注意事项

所有这些都放在您的ForumPost模型中,该模型应该与您的答复具有一对多的关系.

These all go in your ForumPost model, which should have a one-to-many relationship with your replies.

这些方法旨在提供各种各样的功能.不要害怕通读它们并分别进行测试以查看它们在做什么.它们都不是很长,因此这样做并不难.

These are a variety of methods that are meant to provide a wide array of functionality. Don't be afraid to read through them and test them individually to see exactly what they are doing. None of them are very long, so it shouldn't be difficult to do that.

这篇关于Laravel 5-查找模型的分页页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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