How can I get the comment link in a post with paged comments? Like the latest comments link that should point to a comment pagination in a post
Latest comments in homepage for example
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
my single post
- article content
- comments with pagination
my post model
/**
* 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);
}
my comments model
/**
* 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']);
}
my post controller
/**
* 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;
}
my view for post
<article>Post content....</article>
<ol class="comments-list">
@include('site.comments.comment-list', ['comments' => $comments])
</ol>
{!! $comments->paginate !!}
There is no problem with displaying posts and paginated comments, But now I want to show new comments with the link to the post page where that comment is in
controller for displaying latest comments ??
return Comment->where('comment_status' => 1)
->with('user')
->orderBy('comment_date', 'desc')
->take(10)->get();
How do I find the comment page link? I do not know how! Can help me?