I'm new to this forum, apperently it renders the html automaticly, but hopefully you guys get the gist and otherwise could anyone explain to me how I prevent it from doing so and show the code?
Pagination not working
Hi guys
I'm using laravel 6.x. When retrieving a resultset fro the database and usin a simple pagination on it I have some issues. First I was trying to use a post, since the conditions for the query come from a form (it's a users search), but I got an error, I've read online that you can use pagination only with get methods,do I changed my route to a getmethod. Now I'm getting the first page as result, the links are displayed, so I can see how many pages there are, but when I click on the link to go to the second page the result seems to be lost, I get an empty page and the links also only show one link, if I click on that one link the page justs refreshes. First I thought maybe it was because I changed my route so I tried to clear the route's cache, but that didn't make any difference. I tried adding adding $posts->withPath(..) in the controller before returning the results, but the result was still the same. I use the same code for the index pagina (with a different querry ofcourse) of the postroutes and there it works as expected. Anyone an idea what I'm doing wrong?
This is the method in the controller: public function search(Request $request) { $posts = Post::orderBy('id', 'DESC')->where('category_id', DB::table('categories')->where('name', $request->category)->value('id'))->paginate(8); return view('search.searchResults', compact('posts')); }
This is the code in my view:
@if(!$posts->isEmpty()) @foreach($posts as $post)
{{$post->title}}
{{substr($post->body,0, 250)}}
@endforeach @endif<!-- Pagination -->
<ul class="pagination justify-content-center">
{{$posts->links()}}
</ul>
This is my route: Route::get('/search', 'SearchController@search');
ok, still not formatted correctly ``` goes on their own lines.. but whatever
so the only bit that matters is
$posts = Post::orderBy('id', 'DESC')->where('category_id', DB::table('categories')->where('name', $request->category)->value('id'))->paginate(8);
You need $request->category for your query to work on pages 2+
You can pass it along by using the appends() method I suggested.
$posts = Post::orderBy('id', 'DESC')->where('category_id', DB::table('categories')->where('name', $request->category)->value('id'))->paginate(8)->appends(['category'=>$request->category]);
not sure what the purpose of withPath is unless you have a route that matches? Probably need to remove that.
Please or to participate in this conversation.