2
votes

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);
    }
});
});
1
The problem is that on second page your $request does not hold the search term. Do you use GET or POST? (use GET and you will be OK).Kyslik
Can you show us the code where you call this function ??Maraboc
@Kyslik I use GET for the search.spacetravel
And when you click on the 2nd page does the term stay in URL? Show us how you render pagination.Kyslik
@Kyslik No it resets - that is the problem I am trying to fix. I render pagination using just the paginate() command. The $pets are compact into the page.spacetravel

1 Answers

2
votes

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.