mani786's avatar

Larave; Pagination

public function search(Request $request) { $search = $request->input('search'); $category = $request->input('category');

        $results=DB::table('products')
    ->where('name', 'like', "%{$search}%")
    ->select('products.*')
    ->groupBy('name')
    ->where('status','=','on')
    ->paginate(12);
    
    $products = $results;
	$type=$request->type;
    return view('site.products',compact('products','type'));

}

This is a search function and I return that back to products page but when I do that the pagination works as I search something so there comes pagination on the first page but when I go to page 2 then it does not take me to the search's next page rather takes me to the seconf page of overall products page

0 likes
3 replies
Sinnbeck's avatar

You need to ensure that you send the search term on all pagination buttons as well, so they remain in the url

tykus's avatar
tykus
Best Answer
Level 104

I assume you have a GET request for searching; you can append the current search term to the query string:

$results=DB::table('products')
    ->where('name', 'like', "%{$search}%")
    ->select('products.*')
    ->groupBy('name')
    ->where('status','=','on')
    ->paginate(12)
    ->withQueryString();

Please or to participate in this conversation.