I'm currently working on a project where one page displays all items currently in the db, and I use laravel's pagination. This works perfectly fine. But the page also has a search functionality to search for specific items. This is where I encounter my problem.
At the moment the, after the search the first page is displayed fine, but when I press the buttons to go to any other page I get always the first page (the page refreshes but the url doesn't change and I get page 1 again). If I manually change the url to the href on the link it works but pressing the button doesn't change the url. page number is not appending.
Here is my controller
public function searchItems(Request $request)
{
if (!Auth::check() || !Auth::user()->is_admin()) return redirect('/login');
$request->validate([
'admin-query' => 'required|min:3',
]);
$query = $request->input('admin-query');
$items_aux = DB::select("SELECT id, ts_rank_cd(textsearch, query) AS rank FROM \"item\", to_tsquery('english','.$query')
AS query, to_tsvector(name || ' ' || description) AS textsearch WHERE query @@ textsearch ORDER BY rank DESC");
$ids = array();
for ($i = 0; $i < sizeof($items_aux); $i++) {
$ids[$i] = $items_aux[$i]->id;
}
$categories = Category::orderBy('id', 'DESC')->get();
$items = Item::whereIn('id', $ids)->orderBy('id', 'DESC')->paginate(10);
$pagination = $items->appends(array(
'adim-query' => Input::get('admin-query')
));
foreach ($items as $item) {
$img_url = Product_Image::where('id_item', '=', $item->id)->first()['url'];
if (Storage::disk('public')->exists($img_url)) {
$item->img = Storage::url($img_url);
} else {
$item->img = null;
}
}
return view('pages.adminproducts', ['items' => $items, 'categories'=>$categories]);
}
My Routes:
Route::get('admin-items/search', 'AdminItemsController@searchItems');
My Form:
<div class="form col-sm-8 col-md-8 col-lg-4 text-center pt-0">
<!--form-->
<form action="/admin-items/search" method="GET" class="header-search-form" role="search">
<input type="text" name="admin-query" id="admin-query" value="{{ request()->input('admin-query') }}" class="search-box"
placeholder="Search Products">
<button><i class="fa fa-search" aria-hidden="true"></i>
</button>
</form>
</div>
My Items and Links:
<div class="row">
<div class="management-products container-fluid card-deck justify-content-center">
@foreach($items as $item)
@include('partials.admin_product', $item)
@endforeach
</div>
</div>
<div class="row justify-content-center">
<div class="pagination-links">
{{ $items->links() }}
</div>
</div>
</div>
Inspect the links on page loaded
<li class="page-item disabled" aria-disabled="true" aria-label="« Previous">
<span class="page-link" aria-hidden="true">‹</span>
</li>
<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>
<li class="page-item"><a class="page-link" href="http://localhost:8000/admin-items/search?adim-query=volutpat&page=2">2</a></li>
<li class="page-item"><a class="page-link" href="http://localhost:8000/admin-items/search?adim-query=volutpat&page=3">3</a></li>
<li class="page-item"><a class="page-link" href="http://localhost:8000/admin-items/search?adim-query=volutpat&page=4">4</a></li>
<li class="page-item"><a class="page-link" href="http://localhost:8000/admin-items/search?adim-query=volutpat&page=5">5</a></li>
<li class="page-item"><a class="page-link" href="http://localhost:8000/admin-items/search?adim-query=volutpat&page=6">6</a></li>
<li class="page-item"><a class="page-link" href="http://localhost:8000/admin-items/search?adim-query=volutpat&page=2" rel="next" aria-label="Next »">›</a></li>