I have a problem with pagination using use Laravel Scout and TNTSearch driver. For example, I want to search for the keyword "Product 80." The URL is localhost/search?keyword=product+80
and when I go to the next page the URL becomes localhost/search?query=Product%2080&page=2.
It doesn't display anything in the search results. When I manually change the URL to localhost/search?keyword=product+80&page=2
it works fine, how can I fix it?.
Searchbar
<form action="{{ route('products.search') }}" method="get" class="search">
<input class="form-control" type="text" name="keyword" placeholder="Search">
<button type="submit" class="btn"><i class="mdi mdi-magnify"></i></button>
</form>
Route
Route::get('/search', 'ProductController@search')->name('products.search');
Controller
public function search(Request $request)
{
$keyword = $request->keyword;
$products = Product::search($keyword)->paginate(20);
return view('search.index', compact('keyword', 'products'));
}
View
<div class="container">
@foreach ($products as $product)
<div class="col-md-6 col-lg-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ $product->name }}</h5>
</div>
</div>
</div>
@endforeach
{{ $products->links() }}
</div>
dd($request->all())
array:1 [▼
"keyword" => "Product 80"
]
dd($products)
LengthAwarePaginator {#315 ▼
#total: 100
#lastPage: 5
#items: Collection {#340 ▼
#items: array:20 [▼
0 => Product {#363 ▶}
1 => Product {#344 ▶}
2 => Product {#353 ▶}
3 => Product {#361 ▶}
4 => Product {#360 ▶}
5 => Product {#359 ▶}
6 => Product {#358 ▶}
7 => Product {#357 ▶}
8 => Product {#356 ▶}
9 => Product {#355 ▶}
10 => Product {#354 ▶}
11 => Product {#352 ▶}
12 => Product {#362 ▶}
13 => Product {#351 ▶}
14 => Product {#350 ▶}
15 => Product {#349 ▶}
16 => Product {#348 ▶}
17 => Product {#347 ▶}
18 => Product {#346 ▶}
19 => Product {#345 ▶}
]
}
#perPage: 20
#currentPage: 1
#path: "http://marketplace.test/search"
#query: array:1 [▼
"query" => "Product 80"
]
#fragment: null
#pageName: "page"
+onEachSide: 3
}
dd($request->keyword)
"Product 80"
dd($request->query)
ParameterBag {#50 ▼
#parameters: array:1 [▼
"keyword" => "Product 80"
]
}
+
.%20
is a space, so you should be able to split off that as well. – aynberkeyword
toquery
? Can you show us your full blade? – nice_dev$request->keyword
would have beenNULL
. Hence, there were no results. – nice_devdd($request->all());
,dd($products);
,dd($request->keyword);
anddd($request->query);
. – Ahmed Nour Jamal El-Din