Daniel_D's avatar

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)}}

Lees meer

@endforeach @endif
<!-- Pagination -->
<ul class="pagination justify-content-center">
    {{$posts->links()}}
</ul>

This is my route: Route::get('/search', 'SearchController@search');

0 likes
9 replies
Daniel_D's avatar

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?

Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

Daniel_D's avatar

And in my case how would that work? That changes just the url being used, not the result. And what do you mean if you're using a search, every select querry is a search and on the index page it works fine. This is what I do where it works: Controller:

    {
        $posts = Post::orderBy('id', 'DESC')->where('category_id', 1)->where('sub_category_id',2)->paginate(20);
        $categories = Category::all();
        return view('posts.index', compact('posts', 'categories'));
    }```

view:
```  <div class="row">
        @if(!$posts->isEmpty())
            @foreach($posts as $post)
                <div class="col-lg-3 colfmd-4 col-sm-6 mb-4">
                    <div class="card h-100">
                        <a href="#"><img class="card-img-top" src="{{!$post->photos->isEmpty()? $post->photos->first()->path: asset(url('images/design/placeholder.png'))}}" alt=""></a>
                        <div class="card-body">
                            <h4 class="card-title">
                                <a href="{{url('/posts/'.$post->id)}}" style="color: #221E21">{{$post->title}}</a>
                            </h4>
                            <p style="color: #221E21">{{substr($post->body,0, 250)}}</p>
                            <p style="bottom: 0; position: absolute; background-color: #57b846; left:50%; transform: translate(-50%)"><a href="{{url('/posts/'.$post->id)}}"><i class="fa fa-arrow-circle-left"></i>Lees meer<i class="fa fa-arrow-circle-right"></i> </a></p>
                        </div>
                    </div>
                </div>
            @endforeach
        @endif
    </div>
    <!-- /.row -->

    <!-- Pagination -->
    <ul class="pagination justify-content-center">
        {{$posts->links()}}
    </ul>```
Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

Snapey's avatar

This is what I do where it works:

Yes, but I can't see what you do when it does not work. Please edit your original question.

Daniel_D's avatar

I cant edit so I'll repost...

The controller:

    {
        $posts = Post::orderBy('id', 'DESC')->where('category_id', DB::table('categories')->where('name', $request->category)->value('id'))->paginate(8);
        $posts->withPath('search');
        return view('search.searchResults', compact('posts'));´´´
    }

The view
``` <div class="row">
        @if(!$posts->isEmpty())
            @foreach($posts as $post)
                <div class="col-lg-3 colfmd-4 col-sm-6 mb-4">
                    <div class="card h-100">
                        <a href="#"><img class="card-img-top"
                                         src="{{!$post->photos->isEmpty()? $post->photos->first()->path: asset(url('images/design/placeholder.png'))}}"
                                         alt=""></a>
                        <div class="card-body">
                            <h4 class="card-title">
                                <a href="{{url('/sexdating/man_zkt_man/'.$post->id)}}"
                                   style="color: #221E21">{{$post->title}}</a>
                            </h4>
                            <p style="color: #221E21">{{substr($post->body,0, 250)}}</p>
                            <p style="bottom: 0; position: absolute; background-color: #57b846; left:50%; transform: translate(-50%)">
                                <a href="{{url('/sexdating/man_zkt_man/'.$post->id)}}"><i
                                        class="fa fa-arrow-circle-left"></i>Lees meer<i
                                        class="fa fa-arrow-circle-right"></i> </a></p>
                        </div>
                    </div>
                </div>
            @endforeach
        @endif
    </div>
    <!-- /.row -->

    <!-- Pagination -->
    <ul class="pagination justify-content-center">
        {{ $posts->links() }}
    </ul>

</div>´´´


As you see basicly the same
Snapey's avatar
Snapey
Best Answer
Level 122

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.

1 like
Daniel_D's avatar

Thanks Snapey for your patience, that worked, awesome, thanks again... I came across this same problem multiple times on different platforms, when doing a search and no one came up with your solution...

Please or to participate in this conversation.