Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zahedkamal87's avatar

Pagination replacing new search results with old ones when try to change page

I've added a ajax search box in app's home page. It works fine. But when i use the pagination after the new results shows up, pagination leads me to the old results. I'm not sure what I'm not doing right. So looking for expert's help to get a solution for it.

I've these in routes --

Route::get('/', 'BookmarkController@index');
Route::post('/', 'BookmarkController@search');

I've these in their controller -

public function index(Request $request)
{
    $tags_list = Tag::orderBy('tag', 'asc')->get();
    $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->paginate(10);
    $bookmarks_all = Bookmark::orderBy('created_at','desc')->where('public', '1')->get();
    return view('welcome')->with('bookmark', $bookmarks)->with('tags_list', $tags_list)->with('bookmarks_all', $bookmarks_all);
}

public function search(Request $request){
    $search_value = $_POST['search'];
    
    $bookmarks = Bookmark::orderBy('created_at','desc')->where('public', '1')->where('title', 'rlike', $search_value)->orwhere('description', 'rlike', $search_value)->orwhere('contents', 'rlike', $search_value)->orwhere('tags', 'rlike', $search_value)->paginate(10);
    return view('public_bookmarks')->with('bookmark', $bookmarks);
}

In welcome.blade.php

<div class="container content-container">
<div class="row ">
    <div class="col-sm-12">
        <div class="page-header">
            <div class="row">
                <span class="title col-sm-8">Recent Bookmarks</span>
                <form id="demo-2" class="search-form col-sm-4" method="post">
                    {{ csrf_field() }}
                    <div class="input-group">
                        <!-- <input id="search" class="form-control" onkeyup="search_data(this.value, 'result');" placeholder="Search" name="search" value="" type="text"> -->
                        <input id="search" class="form-control" placeholder="Search" name="search" value="" type="text">
                        <span class="input-group-btn">
                            <button class="btn btn-primary" id="search-btn" type="button"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                    <script type="text/javascript">
                        $(document).ready(function() {
                            $('#search-btn').click(function(){
                                $.ajax({
                                    url: '/',
                                    type: "post",
                                    data: {'search':$('input[name=search]').val(), '_token': $('input[name=_token]').val()},
                                    success: function(data){
                                        $('#search-results').html(data);
                                    },

                                    error: function (data) {
                                        console.log('Error on Article extracting');
                                        console.log(data);
                                    }
                                }); 
                            });
                        });
                    </script>
                </form>
            </div>
        </div>
    </div>
    <div class="col-sm-9" id="search-results">
        
        @include ('public_bookmarks')
    </div>
</div>

In public_bookmars.blade.php

@if (count($bookmark) > 0)
<div class="row card-row">
@foreach ($bookmark as $bookmark_single)
    <div class="col-sm-4 col-xs-12 card-parent" data-col="col-sm-4">
        <div class="card">
            <div class="card-part1">
                <div class="img-card">
                    <img src="{{$bookmark_single->thumbnail}}" />
                </div>
                <div class="card-content">
                    <h4 class="card-title">
                        {{ $bookmark_single->title }}
                    </h4>
                    <div class="card-desc">
                        {{ str_limit($bookmark_single->description, $limit = 50, $end = ' [...]') }}
                    </div>
                </div>
                <div class="card-read-more">
                    <p><?php   $tags = $bookmark_single->tags;
                        $tag_list = explode(',', $tags); ?>
                        @foreach ($tag_list as $tag)
                            <a href="/tag/{{$tag}}" class="label label-primary">{{$tag}}</a>
                        @endforeach
                    </p>
                    <p class="card-user">- &nbsp;<a href="/user/{{ $bookmark_single->bookmarker }}">{{ $bookmark_single->bookmarker }}</a></p>
                    <a class="v-link" target="_blank" href="{{ $bookmark_single->url }}">Visit the link</a>
                </div>
                <button type="button" class="btn btn-success btn-circle btn-lg btn-read-more"><i class="fa fa-chevron-right"></i></button>
            </div>

            <div class="card-part2 col-xs-0">
                {{ print $bookmark_single->contents }}
            </div>
        </div>
    </div>
@endforeach
</div>
{{ $bookmark->links() }}
@endif
0 likes
3 replies
jlrdw's avatar

Why make your life hard just paginate regular and not ajax.

zahedkamal87's avatar

Facing same issue with normal pagination. Returns me to old results when i click on pagination

jlrdw's avatar

Make sure you are properly appending anything to pagination links. Go over the example in the docs again.

Appending To Pagination Links

You may add to the query string of pagination links using the appends method. For example, to append &sort=votes to each pagination link, you should make the following call to appends:

{{ $users->appends(['sort' => 'votes'])->links() }}

Please or to participate in this conversation.