spacetravel
spacetravel

Reputation: 125

Laravel - Paginate and Append Search Result

This is probably has a very simple solution, but I have not been able to solve it. I need to paginate my search results, so when I go onto my next page, it does not reset my search results. Here is what I am trying to paginate:

$pets = DB:table('pets')
    ->where(function($q) use ($request){
        $q->orWhere('name','LIKE','%'.$request->search.'%')
})
->paginate(40)

The code posted above searches my database. I am able to paginate it using paginate(40) as shown above. The only thing is that the search resets when I go on to page two, for example. What I want is for the search to be applied to all the pages.

I tried using appends() with $pets, $q and $request, which none worked. I have also tried some answers from here, which yielded no results.

From the Laravel documentation I am guessing, your supposed to use appends() to apply the search to all the pages?

EDIT:

@foreach($pets as $pet)
 <p>{{$pet->name}}</p>
@endforeach

Pagination:

{{$pet->links()}}

AJAX Call:

$("#filter").change(function() {
$value=$(this).val();
  $.ajax({
    type: "get",
    url: "{{$pets->name}}",
    data: {'search':$value},
    success: function(data){
      $('#results').html(data);
    }
});
});

Upvotes: 3

Views: 2538

Answers (1)

Kyslik
Kyslik

Reputation: 8385

In your view use something like this:

{!! $pets->appends(\Request::except('page'))->render() !!}

or

{{ $pets->appends(\Request::except('page'))->links() }}

Code above will create pagination links and add all parameters except page in it.

Upvotes: 2

Related Questions