Pagination problem in search option in Laravel

Published 1 week ago by AbdulBazith

I am having a textbox which searches the data and displays in tabular format. The search box works when I type any text it in that is it works in “Onkeyup” event written in Ajax .

When the view page loads all the data are displayed to the user. I have given pagination for the view.

When the user type any name in the search box it goes to the function in controller by Ajax call and searches for the result and then displays only the matched values to the user. But the pagination remains same as for the first view. If I change the page number then the page is refreshed. How to solve this?

This is my controller Sales_detailsController

public function index()
        $sales = Sales_details::orderBy('created_at','desc')->paginate(5);

        return view('Sales_details.view')->withsales($sales);

This is my view. Blade


@section('title','| All data Sales')


<div class="row">
        <div class="col-md-10">

     <div class="col-md-2">
            <a href="{{ route('Sales_details.create') }}" class="btn btn-lg btn-block btn-primary btn-h1-spacing"> Sales Entry</a>
</div><!-- end of row-->

<div class="row">
<div class="col-md-12">

        <caption><h2 align="center">SALES ENTRY PREVIEW</h2></caption>

{{ Form::text('search_a',null ,array('class'=>'form-control','id'=>'search_a','name'=>'search_a')) }}<br>

    <table  id="sales_details" class="table table-bordered">

              <th >Sales Id</th>
              <th>Customer Id</th>
              <th>Customer Type</th>
              <th>Customer Name</th>
              <th>Customer Area</th>
              <th>Customer Booth</th>
              <th>No of Litres</th>


            @foreach($sales as $sal)
                    {{-- <th>{{ $ven->id }}</th> --}}
                    <td>{{ $sal->sales_id }}</td>
                    <td>{{ $sal->date }}</td>
                    <td>{{ $sal->time }}</td>
                    <td>{{ $sal->customer_id }}</td>
                    <td>{{ $sal->customer_type }}</td>
                    <td>{{ $sal->customer_name }}</td>
                    <td>{{ $sal->customer_area }}</td>
                    <td>{{ $sal->customer_booth }}</td>
                    <td>{{ $sal->rate_per_litre }}</td>
                    <td>{{ $sal->no_of_litre }}</td>
                    <td>{{ $sal->total }}</td>
                    <td>{{ $sal->note }}</td>

        <a href="{{ route('',$sal->id) }}" class="btn btn-success btn-sm" style="width: 100%;">View</a>


            <div class="text-center">             

                {!! $sales->Links(); !!} 

<script type="text/javascript"> 




type : 'get',

url : '{{URL::to('search_name')}}',










This is my java script

<script type="text/javascript"> 




type : 'get',

url : '{{URL::to('search')}}',









This is my controller for search the function search()

public function search( Request $request )






                    foreach ($sales as $key => $sale)















                    ."<a href='' class='btn btn-success btn-sm' style='width: 100%;'>View</a>".




                    return Response($output);


When page load the pagination is ok. but after search the pagination is not changing. the old pagination is displayed and if i change the page after search once again it reloads the page so that my searched keyword disappears.

How to solve this??


If you want to apply filters to the next page you should add them to your paginator like this:

$product = Product::where('naam', 'LIKE', '%' . $q . '%')
        ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
$product->appends(['search' => $q]);
And change your route from post to get:

Route::get('search', '[email protected]');


@pardeepkumar it not working.. after filter also the old pagination is displayed. before filter my records are more then 50 so i displayed just 5 per page that is paginate(5) so it took 10 pages.

But after filter my record will be just 6 or 7. i have given paginate(10). so here it must be in only one page with 7 records.

but it shows 10 pages i think after filter the pagination not working


I would go with query scope

but you can checkout the following links

QueryScope Example: or give this a try... on mobile haven't tested but the logic is sound so you might not run into any problem... it readable which is supposed to be... any problems let me know.. cheers...


public function index(Request $request)
        $products = Product::search($request->keyword)
        return view('products._searchResult',compact('products'));

    return view('products.index', compact('products'));


Route::resource('products', 'ProductsController');
//Product Model

public function scopeSearch($query,$keyword)
    $query->where('name','like',"%$keyword%");// add as many column searchs as you wants 



        @foreach($products as $product)
            <li>{{ $product->name }}</li>
            <li>{{ $product->category->name }}</li>
            <li>{{ $product->package->type }}</li>
{!! $product->links() !!}

<div class="form-group">
    <input type="text" name="keyword" class="form-control" id="keyword" />

<div id="list">

        var search = $(this).val();
            url: '{{ route('products.index') }}',
            type: 'get',
            data: {keyword: 'search'},
        .done(function(data) {
        .fail(function() {
        .always(function() {


@BezhanSalleh thank you, wait i will try and reply

Please sign in or create an account to participate in this conversation.