migdalius's avatar

Pagination and search

I have standard search function

public function kategorie(Request $request)
    {

        $query = $request->input('query');        
        
        $pages = Page::where('kategoria', 'like', "%$query%")
                      ->orWhere('tags', 'like', "%$query%")
                      ->paginate(10);

        return view('kategoria')->with('pages', $pages);
    }

in view i have standard laravel pagination

{{ $pages->links() }}

When i use search like:

 ?query=somepage

Paginaton show me 2 pages, in first it's ok i see 10 search results, but when i wana click second page they show me query

?page=2

and of is 10 reults, but they show all results not only with query "somepage"

how to make results like

?query=somepage&page=2
0 likes
17 replies
lacasera's avatar

you can do this {{ $pages->appends(['query' => 'somepage'])->links() }}

migdalius's avatar

Thanks, but i was wrong

?query=somepage&page=2

this is not good solution, they filters all query but pagination is still work just show 1-10 results on page, and ignore rest (just dont show) only show pagnation (20+ page)

BryanK's avatar
BryanK
Best Answer
Level 16

try returning your view like this instead of using with:

return view('kategoria')->compact('pages');
jlrdw's avatar

Can you show your view code. And route.

migdalius's avatar

this work correct :

return view('kategoria')->compact('pages');

But when i dont wana static like:

{{ $pages->appends(['query' => 'somepage'])->links() }}

i change it for

{{ $pages->appends(['query' => '$query'])->links() }}

but dont work correct, when i change pagination to second page its show:

?query=%24query&page=2

BryanK's avatar

What are you trying to do exactly? Why are you trying to overwrite the query var with something else?

jlrdw's avatar

Why the apostrophes around '$query'

migdalius's avatar

Sorry im chaotic, i have search form when i start search query like "abc" or "bac", search check category and tags in pages like %query%, but if i have more than 50 restults and pagiantion (10) problem starts with pagination. Like <1><2><3><..><5> when i click they show me just all page not only with this query like "abc"

lacasera show me good solution

{{ $pages->appends(['query' => 'abc'])->links() }}

It's work but, they will always filter abc and i wana output $query like "bac" etc. some like wild cart

BryanK's avatar
return view('kategoria')->compact('pages', 'query');

Remove the quotes like @jlrdw suggested:

{{ $pages->appends(['query' => $query])->links() }} 

I assume you were getting an error that $query was an unknown var which is why you quoted it. compacting query will fix that.

migdalius's avatar

I try this early

 public function kategorie(Request $request)
    {

        $query = $request->input('query');        
        
        $pages = Page::where('kategoria', 'like', "%$query%")
                      ->orWhere('tags', 'like', "%$query%")
                      ->paginate(5);

     
        return view('kategoria',compact('pages','query'));
    }
{{ $pages->appends(['query' => $query])->links() }}

And results is :

Undefined variable: query (View:

Results

return view('kategoria')->compact('pages', 'query');

i change but still Undefined variable: query, well i will try find why its dont pass to view.

BryanK's avatar

Sorry, should be:

return view('kategoria')->compact('pages')->with('query', $query);
migdalius's avatar

well, dont work :(, but thanks for help im grateful.

BryanK's avatar

It should work. I forgot that query was just a string, so you can't compact it. You should be able to define it using with('query', $query) and use $query in the view.

migdalius's avatar

unfortunately, dont work

public function kategorie(Request $request)
    {

        $query = $request->input('query');        
        
        $pages = Page::where('kategoria', 'like', "%$query%")
                      ->orWhere('tags', 'like', "%$query%")
                      ->paginate(5);

     
        
        
        return view('kategoria')->compact('pages')->with('query', $query);
    }

and view

{{ $pages->appends(['query' => $query])->links() }}

It's always show Undefined variable: query

BryanK's avatar

try putting this at the top of the kategoria.blade.php to make sure nothing else is conflicting.

{{ dump( $pages->appends(['query' => $query]) ) }}
{{ dd($query) }}

You should get the LengthAwarePaginator instance and the query string.

LengthAwarePaginator {#255 ▼
  #total: 10
  #lastPage: 2
  #items: Collection {#264 ▶}
  #perPage: 5
  #currentPage: 1
  #path: "http://testing.test/pages"
  #query: array:1 [▼
    "query" => "something"
  ]
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▶]
}
‹
1
2
›
"something"
migdalius's avatar
ErrorException {#745 ▼
  #message: "Undefined variable: query"
  #code: 0
  #file: "/home/mitek91/domains/test91.vot.pl/public_html/obito/storage/framework/views/653b682019bed23b6b130f5076f5c885047247fb.php"
  #line: 1
  #severity: E_NOTICE
}
1

I find solution this problem

{{ $pages->appends($_GET)->links() }}

Please or to participate in this conversation.