Is the ‘page number’ in the URL equivalent to the post ID in the database and used to fetch the post? Because otherwise, it’s not really going to do anything.
Single Page Pagination
Hello Everyone! I am using pagination in my project on the posts page, which works fine, but I want to have pagination on a single post page, where I can go back and forth for another single post. I already have pagination links on a single post page, but on clicking next it just refreshes and stays on the same post, however, the page number changes in the URL. Thanks
@kokoshneta Hi thanks for replying. initially, when I load the single post page the URL is "http://127.0.0.1:8000/posts/post/repellat-voluptatem-accusantium-quis" and upon hitting the next pagination button it becomes "http://127.0.0.1:8000/posts/post/repellat-voluptatem-accusantium-quis?page=2" and the code is below Route file
Route::get('/posts/post/{post:slug}', [PostController::class, 'post']);
Controller file
public function post(Post $post){
return view('post', [
'post' => $post,
'posts' => Post::paginate(1)
]);
}
so please have a look at it and guide me on making it work.
@abbasrahim723 Well, that obviously won’t work. That will show you page 2 of the post Repellat voluptatem accusantium quis, which doesn’t exist, since each post is just one ‘page’.
Pagination is for when you load a long list of things and you break that list up into smaller parts. Loading a single post is not a list at all, and an individual post has not concept of ‘previous’ or ‘next’.
You’ll have to manually make your own pagination logic – and it will need to be pagination logic, because how do you define the ‘next’ or ‘previous’ post to any given post? Do you order them alphabetically by title? Or by their creation timestamp? Or by when they were published? And if your posts can belong to different categories, should the ‘previous/next’ post always be one from the same category?
These and many others are things Laravel cannot possibly know, because you need to decide them. Then you load the ‘previous’ and ‘next’ post in your controller:
// Assuming you have a standard resource controller
class PostController {
public function show(Post $post) {
$previous = $post->getPreviousPost();
$next = $post->getNextPost();
return view('post', [
'post' => $post,
'previous' => $previous,
'next' => $next
]);
}
}
Of course, you need to write the getPreviousPost() and getNextPost() methods in your Post class – that’s where you define what you actually mean by ‘previous’ and ‘next’.
how did you fetch the single post. is it
$post = Post::find($id)->paginate(1)
If yes, then it is wrong
@TimiAde thanks for replying. yes it is identical to what you have written and is mentioned below
public function post(Post $post){
return view('post', [
'post' => $post,
'posts' => Post::paginate(1)
]);
}
so please let me know how to make it work.
@abbasrahim723 Hey , for this scenario you will need to create a pagination manually which will change the "id" of the post in url not the "page=n" query.
http://localhost:8000/posts/4?page=2
on using default pagination which laravel provides. It will change the page number but not the id of post which will keep you with the same data with a page refresh. I hope you get the problem you need to solve.
I did it , but now the problem is I couldn't find out how to know wether a post is last or first in order to enable or disable the link for the last and first post, because it then says " Attempt to read property "slug" on null", so please help me out.
controller file
public function post(Post $post){
$previous = $post::where('id', '<', $post->id)->orderBy('id','desc')->first();
$next = $post::where('id', '>', $post->id)->orderBy('id','asc')->first();
return view('post', [
'post' => $post,
'previous' => $previous,
'next' => $next
]);
}
Blade file
<div class="other-posts">
<div class="left" >
<h5>{{ $previous->title }}</h5>
@if($previous)
<a href="/posts/post/{{ $previous->slug }}">Prev Post</a>
@endif
</div>
<div class="right" >
<h5>{{ $next->title }}</h5>
@if($next)
<a href="/posts/post/{{ $next->slug }}">Next Post</a>
@endif
</div>
</div>
Thanks, everyone for participating. I tried isset function and it worked, now everything is working fine
Please or to participate in this conversation.