Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5-查找模型的分页页面_Php_Laravel_Laravel 5_Laravel Pagination - Fatal编程技术网

Php Laravel 5-查找模型的分页页面

Php Laravel 5-查找模型的分页页面,php,laravel,laravel-5,laravel-pagination,Php,Laravel,Laravel 5,Laravel Pagination,我正致力于建立一个基本论坛(灵感来源于)。当用户向线程发布回复时: 我想用他们的回复锚(与Laracast的行为相同)将他们引导到分页回复列表的末尾 我还想让用户在编辑回复时返回到正确的页面 如何确定新回复将发布到哪个页面(?page=x),以及如何在编辑回复后返回到正确的页面?或者,在主帖子列表中,最新回复出现在哪一页 这是我目前的ForumPost模型(减去一些不相关的东西)- 从根本上讲,您使用的是两个值:第一,回复的索引与一篇文章的所有回复相关,第二,页面中的回复数 例如,您可能有一

我正致力于建立一个基本论坛(灵感来源于)。当用户向线程发布回复时:

  • 我想用他们的回复锚(与Laracast的行为相同)将他们引导到分页回复列表的末尾
  • 我还想让用户在编辑回复时返回到正确的页面
如何确定新回复将发布到哪个页面(
?page=x
),以及如何在编辑回复后返回到正确的页面?或者,在主帖子列表中,最新回复出现在哪一页

这是我目前的
ForumPost
模型(减去一些不相关的东西)-


从根本上讲,您使用的是两个值:第一,回复的索引与一篇文章的所有回复相关,第二,页面中的回复数

例如,您可能有一个id为301的回复。不过,这是针对某个特定帖子的第21条回复。你需要想办法弄清楚这是第21条回复。这实际上是相对简单的:您只需计算有多少回复与该帖子相关,但ID较小

//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;
}
好的,现在你只需要拉那个页面。这只需要一种方法,在该方法中,您可以指定所需的页码以及一个页面的回复数量。然后,该方法可以计算偏移量和限制,并检索所需的记录

public function getPageOfReplies($pageNumber, $repliesPerPage)
{
    $pageOfReplies = $this->replies()->offset($pageNumber*$repliesPerPage)->limit($repliesPerPage)->get();
    return $pageOfReplies;
}
不过,为了更好地衡量,我们可以构建一个方法来获取最终回复的索引

public function getLastReplyIndex()
{
    $count = $this->replies()->count();
    return $count;
}
太好了!现在我们有了所有我们需要的积木。我们可以构建一些简单的方法,使用更通用的方法轻松检索我们需要的数据

让我们从一个方法开始,该方法获取整个页面的回复,其中只有一个回复(请随意更改名称(我假设每页有10个回复)):

为了更好地衡量,我们可以制作一个方法,获取最终回复页面

public function getFinalReplyPage()
{
    $repliesPerPage = 10;
    $index = $this->getLastReplyIndex();
    $pageNumber = $this->getPageNumber($index, $repliesPerPage);
    return $this->getPageOfReplies($pageNumber, $repliesPerPage);
}
您可以构建各种其他方法来使用我们的构建块方法,并在页面之间跳转,在回复之后或之前获取页面,等等

几个音符

这些都在您的
ForumPost
模型中,它应该与您的回复有一对多的关系


这些是各种各样的方法,旨在提供广泛的功能。不要害怕通读它们并单独测试它们,以了解它们到底在做什么。它们都不是很长,所以这样做应该不难。

以下是我的想法。如果有人对此有任何改进建议,请告诉我。我真的很想知道是否有一个更拉威尔的方式来做到这一点,我真的很感激杰弗里的方式分享他的秘密,因为他正在做这件事在拉腊卡斯特

/**
     * Get Reply Page
     * 
     * Returns the page number where a reply resides as it relates to pagination
     * 
     * @param null $replyId Optional ID for specific reply
     * @param bool $pageLink If True, return a GET parameter ?page=x
     * @param int $paginate Number of posts per page
     * @return int|null|string // Int for only the page number, null if no replies, String if $pageLink == true
     */
    public function getReplyPage($replyId = null, $pageLink = false, $paginate = 20)
    {
        // Find the page for a specific reply if provided, otherwise find the most 
        // recent post's ID. If there are no replies, return null and exit.
        if (!$replyId) {
            $latestReply = $this->latestReply();
            if ($latestReply) {
                $replyId = $latestReply->id;
            } else {
                return null;
            }
        }

        // Find the number of replies to this Post prior to the one in question
        $count = $this->replies()->where('id', '<', $replyId)->count();

        $page = CEIL($count / $paginate +1);

        // Return either the integer or URL parameter
        return $pageLink ? "?page=$page" : $page;
    }
/**
*获取回复页面
* 
*返回与分页相关的回复所在的页码
* 
*@param null$replyId特定回复的可选ID
*@param bool$pageLink如果为True,则返回一个GET参数?page=x
*@param int$paginate每页的帖子数
*@return int | null | string//int仅用于页码,如果没有回复则为null,如果$pageLink==true则为string
*/
公共函数getReplyPage($replyId=null,$pageLink=false,$paginate=20)
{
//查找特定回复(如果提供)的页面,否则查找最
//最近帖子的ID。如果没有回复,则返回null并退出。
如果(!$replyId){
$latestReply=$this->latestReply();
如果($latestReply){
$replyId=$latestReply->id;
}否则{
返回null;
}
}
//请先查找对此帖子的回复数,然后再查找相关回复数

$count=$this->repress()->其中('id','请您提供一个例子好吗?一个问题是在用户编辑回复后返回回复所在的页面-例如,回复是第2页(共2页)上的第一个条目。如果有人从第1页删除回复,正在编辑的回复现在将位于第1页而不是第2页。因此,我需要立即获得正确的页面在保存新回复或编辑的回复后。感谢您提供的示例,除了找出某个特定回复位于哪个页面之外,我还喜欢它。有什么想法吗?谢谢您的帮助,我必须回家,但我会在几个小时后再次检查此问题。:)另外,还有一个关键因素——我需要从主帖子索引中知道回复所在的页面(如laracasts.com上所示)。因此,我必须有一种方法,能够在创建、更新和索引过程中动态获取回复的确切页面。哇,这是很多函数!我已经阅读了您的答案好几次,但仍在尝试将其拼凑在一起。一种方法,
getReplyOffset()
,缺失或可能名称错误-找不到任何真正匹配的东西。@NightMICU这很酷。函数中的问题是,在方程执行之前要强制转换索引…我将方程括在括号中,现在它按预期工作。从概念层面上看,我认为模型没有理由重新计算rn一个链接-当然,除非它是跟踪一堆链接的表的模型。此外,自动分配每个参数是危险的领域。定义一个所有事情都必须遵循的刚性接口,你以后会感谢你自己。最后,在这种情况下没有理由使用for循环。这种问题有整数除法em.欣赏这些想法。这远不是完美的(for循环是深夜思考,当然除法是更好的解决方案)。我最关心的是知道回复所在的页面,以便我可以直接链接到该回复-这就是我将其包含在方法中的原因。这有助于减少内联代码的数量以实现该目标。对更新的评论:
$count%$pagi
public function getLastReplyIndex()
{
    $count = $this->replies()->count();
    return $count;
}
public function getPageThatReplyIsOn($replyId)
{
    $repliesPerPage = 10;
    $index = $this->getReplyIndex($replyId);
    $pageNumber = $this->getPageNumber($index, $repliesPerPage);
    return $this->getPageOfReplies($pageNumber, $repliesPerPage);
}
public function getFinalReplyPage()
{
    $repliesPerPage = 10;
    $index = $this->getLastReplyIndex();
    $pageNumber = $this->getPageNumber($index, $repliesPerPage);
    return $this->getPageOfReplies($pageNumber, $repliesPerPage);
}
/**
     * Get Reply Page
     * 
     * Returns the page number where a reply resides as it relates to pagination
     * 
     * @param null $replyId Optional ID for specific reply
     * @param bool $pageLink If True, return a GET parameter ?page=x
     * @param int $paginate Number of posts per page
     * @return int|null|string // Int for only the page number, null if no replies, String if $pageLink == true
     */
    public function getReplyPage($replyId = null, $pageLink = false, $paginate = 20)
    {
        // Find the page for a specific reply if provided, otherwise find the most 
        // recent post's ID. If there are no replies, return null and exit.
        if (!$replyId) {
            $latestReply = $this->latestReply();
            if ($latestReply) {
                $replyId = $latestReply->id;
            } else {
                return null;
            }
        }

        // Find the number of replies to this Post prior to the one in question
        $count = $this->replies()->where('id', '<', $replyId)->count();

        $page = CEIL($count / $paginate +1);

        // Return either the integer or URL parameter
        return $pageLink ? "?page=$page" : $page;
    }