【问题标题】:how to generate link from paginated model in laravel如何从 laravel 中的分页模型生成链接
【发布时间】:2021-08-03 23:15:14
【问题描述】:

如何在带有分页 cmets 的帖子中获取评论链接? 就像最新的 cmets 链接应该指向帖子中的评论分页

例如首页最新的cmets

1- link: post/1/comment-page-3/#comment-300
2- link: post/103/comment-page-2/#comment-299
3- link: post/24/comment-page-6/#comment-298
4- link: post/11/comment-page-1/#comment-297

我的单篇文章

  • 文章内容
  • 带有分页的cmets

我的帖子模型

 /**
 * Get all of the comments for the Posts
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function comments()
{
    return $this->hasMany(Comment::class, 'post_id', 'id')
    ->whereNull('comment_parent')->where('comment_status', 1)
    ->with(['replies', 'user']);
}

public function scopeType($query, $type)
{
    return $query->wherePost_type($type);
}

我的 cmets 模型

    /**
 * Get the post that owns the Comment
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function post()
{
    return $this->belongsTo(Posts::class, 'post_id', 'id');
}


/**
 * Get all of the children for the Comment
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function replies()
{
    return $this->hasMany(Comment::class, 'comment_parent', 'id')->with(['replies', 'user']);
}

我的后期控制器

    /**
     * get and show the post
     * 
     * @param int $id requested from user
     * @param string $slug requested from user
     * @param string $page comment page requested from user
     * 
     * @return view
     */
    public function show_post($id, $post_slug = '', $comment_page_slug = '') 
    {
        
        //get post by requested id
        $post = Posts::Type('post')->with('user')->findorfail($id);

        //get post comment with paginate
        $comments = $this->get_comments_with_pagination($post, $comment_page_slug);

        //check slug request is current in db
        if ($post_slug && strtolower($post->slug) != strtolower(urlencode($post_slug)) ) 
        {
            abort(404);
        }

        //check comments working and curent paginate
        if (! $comments) {
            abort(404);
        }

        //show post with comments
        return view('site.post.show-post', ['post' => $post, 'comments' => $comments]);

    }

 /**
     * get comments and pagination url
     * 
     * @param object $post
     * 
     * @param string $page
     * 
     * @return object comments with custom paination
     */
    private function get_comments_with_pagination($post, $page) {

        //check pagination comment is current old wordpress
        $matches = array();
 
        if ($page && ! preg_match ( '/comment-page-([0-9]+)/', $page, $matches ) )
        {
            return false;
        }

        if ($matches) 
        {
            $page = $matches[1];
        }
        else 
        {
            $page = 1;
        }

        //calculate skiping comment by page number
        $skip = ($page * $this->comment_paginate) - $this->comment_paginate;

        //get comments
        $comments = $post->comments()->skip($skip)->take($this->comment_paginate)->orderBy('comment_date','desc')->get();

        //comment number
        $count = $post->comments->count();

        //create pagination template
        $page_num = round($count / $this->comment_paginate) + 1;
        $paginate_url = '';

            if ($page_num > 0 ) {
                
                for($i = 1; $i < $page_num; $i++)
                {
                    if ($i == $page) {
                        $paginate_url .= '<li class="page-numbers current"><span>'.$i.'</span></li>';
                   
                    } else {
                        
                        $paginate_url .= '<li class="page-numbers"><a href="' .route('post.show', [$post->id, $post->slug]). '/comment-page-' . $i. '/#comments">'.$i.'</a>';
             
                    }
                }

            }


        $comments->paginate = '<ul class="modami-paginate">'.$paginate_url.'</ul>';
        $comments->number = $count;

        return $comments;

    }

我对帖子的看法

<article>Post content....</article>
        
        <ol class="comments-list">
        @include('site.comments.comment-list', ['comments' => $comments])
        </ol>
       
        {!! $comments->paginate !!}

显示帖子和分页的 cmets 没有问题,但现在我想显示新的 cmets,并带有指向该评论所在的帖子页面的链接

用于显示最新 cmets 的控制器 ??

return Comment->where('comment_status' => 1)
        ->with('user')
        ->orderBy('comment_date', 'desc')
        ->take(10)->get();

如何找到评论页面链接? 我不知道怎么!可以帮帮我吗?

【问题讨论】:

    标签: php laravel pagination


    【解决方案1】:

    它不一定发生在模型或查询中。在视图中为新的 cmets 创建链接。根据关系找到评论的 id,然后创建一个路由以通过 id 显示帖子。在视图中,使用 route() 方法创建一个带有 href 的标签,指向相关的分页帖子。 在您的代码中的任何地方,如果您有分页评论模型,您可以通过以下方式轻松获取最后页码:

        $comment->lastPage()
    

    您可以通过 $cmets->getPerPage() 来检查添加新评论是否添加新页面,这是默认的 perpage 和 $cmets->count()。因此,如果计数为 20,每页为 10,则添加新评论会创建新页面,即 3。

    但是,如果您无法访问分页 cmets,您可以将这个简单的方法添加到您的评论模型中

        public function page($perPage = null)
        {
            $perPage = $perPage ?: $this->getPerPage();
            $index = self::where('post_id', $this->post_id)->where('id', '<', $this->id)->count();
            $page = (int) ($index / $perPage) + 1;
            return route('post.show', ['post' => $this->post_id, 'page' => $page]);
        }
    

    现在任何评论模型都有指向帖子页面的链接,其中 cmets 被分页到评论模型

    <a href="{{$comment->page()}}">SHOW COMMENT IN POST</a>
    

    编辑

    请记住,分页器使用偏移量和限制。所以你只需要知道你的记录在模型上的位置。如果您有 25 条记录,并且您想转到第 17 条评论的页面,并且您的 perpage 为 10,则下面创建的链接将完成这项工作:

       route('post.index',['page'=>2]);
    

    假设您没有更改模型查询的分页方法的 $pageName。

    【讨论】:

    • 我可以通过 route() 和 post id 找到帖子链接。但是post cmets是分页的。如何在帖子中找到评论页面?
    • 例如,一个人用分页cmets回复了帖子第7页的评论。此回复显示为最新评论。现在,通过单击它,应该参考该帖子的第 7 页。因为post cmets是分页的
    • 我在这个链接(stackoverflow.com/a/29155685/5149704)中找到了答案。但这不是很好的性能,我仍在寻找更好的解决方案
    • 首先我需要找到当前评论之前的 cmets 数量(每个评论一个额外的查询?)。然后我需要将此数字除以分页数。获得的数字是当前评论页面。它不好,但我试试这个
    • 循环中每个评论的额外查询?。 $this->post->cmets->where('id', 'id)->count();谢谢。这是我的问题,但我不知道这个解决方案是否性能最佳
    猜你喜欢
    • 2014-05-28
    • 2015-07-27
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    相关资源
    最近更新 更多