1
votes

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"
  ]
}
2
Can you show your Product::search function? I'm guessing it might be trying to split off only +. %20 is a space, so you should be able to split off that as well.aynber
How did query parameter change from keyword to query? Can you show us your full blade?nice_dev
Due to this change aforementioned, $request->keyword would have been NULL. Hence, there were no results.nice_dev
@aynber I use tntsearch, I don't know which function you meanIndra Sukmajaya
please post the output of dd($request->all());, dd($products);, dd($request->keyword); and dd($request->query);.Ahmed Nour Jamal El-Din

2 Answers

0
votes

In order for the pagination to work correctly, Laravel requires a certain query name. So simply change your html to:

<input class="form-control" type="text" name="query" placeholder="Search">

and in your controller:

$keyword = $request->query;
0
votes

This should work:

public function results(Request $request)
{
    $request->flashOnly('q');
    $products = Product::search($request->q)->paginate(5);
    $procucts->withPath('results');
}

Take a look here Laravel Documentation Requests