Laravel - Paginate and Append Search Result

Viewed 2141

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 Answers
Related