I have just started with Laravel and I am trying to make a search function with proper pagination. The function works for page one but on pag
a quick way in view (Lavarel 5.7)
$product->appends(Request::all())->links();
Route::get('product', function () {
$product= App\product::paginate(15);
$product->setPath('custom/url');
});
View:
{{ $product->appends(['search' => Request::get('page')])->links() }}
In your view file where you display pagination...
{{ $results->appends(Request::except('page'))->links() }}
appends keeps the query string value except "page".
If you are using search form with GET method then use something like these to preserve pagination withing search results.
public function filter(Request $request)
{
$filter = $request->only('name_operator','name_value','email_operator','email_value', 'phone_operator','phone_value', 'gender_value', 'age_operator','age_value');
$contacts = $this->repo->getFilteredList(array_filter($filter));
$contacts->appends($filter)->links(); //Continue pagination with results
return view('dashboard::index', compact('contacts'))->withInput($request->all());
}
$searchdata = \Request::get( 'inputTextFieldname' ); \make as global
$searchresult = Modelname::where ( 'blogpost_title', 'LIKE', '%' .$searchdata . '%' )->paginate(2);
return view( 'search', compact('searchresult') );
and in your view page
{{$searchresult->appends(Request::only('inputTextFieldname'))->links()}}
make your route to get method
Route::get('/search', ['as' => 'search', 'uses' => 'searchController@index']);
this will be done, thanks,
use any in route Instead post Route::any('search', 'IndexController@search');