Sidart's avatar
Level 12

Pagination result change when user changes pagination page

Hello,

I created this function witch is searching in a category based on a value a user have provided.

public function specific(Request $request, $catname)
   {
   // Get the category name
    $category = Category::where('name',$catname)->first();
  //  Get all parent categories
        $categories = Category::where('p_id',0)->with('childs')->get();
 //  User search query
        $search = $request->search;
 // get the records that much the query provided and  the category
        $artwork = Artwork::where('title','like','%'.$search.'%')
                     ->where('description','like','%'.$search.'%')
                     ->whereHas('category',function($q) use ($catname) {
                          $q->where('name',$catname);
        })->paginate(1);
   
        return view('pages.search.category')->withCategory($category)
                                       ->withArtwork($artwork)
                                       ->withCategories($categories);
     }

I am displaying the paginator like this:

{!! $artwork->links() !!}

The issue is that if i do a first search the result are correct and for my specific test it cames back with 2 total pages and 2 records but when i click on the second page for some reason it adds one more page and loads all the records from DB!

I think the issue is somewhere in my $arwork query but i cant figure out what i am doing wrong!

Thank you in advance!

0 likes
2 replies
Sidart's avatar
Sidart
OP
Best Answer
Level 12

I found the solution searching in the docs.

This should be the way to display the pagination:

    {{ $artwork->appends(['search' => request()->query('search')])->links() }}

Please or to participate in this conversation.