1

Trying to set up basic search functionality for products. I am having trouble sorting the route parameter variable and passing the query string to the search function.

Route::get('/search/{query?}', 'ProductController@searchable');

This works and returns a query when I input the query manually.

Controller

public function searchable($query)
{
    // search database, with result, list on page, with links to products,
    $products = Product::search($query)->get();

    return view('search.index', compact('products'));
}

However, I would like it to come from the URL /search?test.

My form shows:

{{ Form::open(array('action' => 'ProductController@searchable', 'method' => 'get', 'files' => 'false')) }}
<input type="search" name="search" placeholder="type keyword(s) here" />
<button type="submit" class="btn btn-primary">Search</button>
{{ Form::close() }}`

I am new to Laravel and need a little help. I am using Laravel Scout and TNTSearch.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
m33bo
  • 1,334
  • 1
  • 17
  • 34

1 Answers1

3

You don't need to user {wildcard} for searching. We have Request for that

Route::get('search', 'ProductController@searchable');

Pass the url instead.

{{ Form::open(array('url' => 'search', 'method' => 'GET', 'files' => 'false')) }}
    <input type="search" name="search" placeholder="type keyword(s) here" />
    <button type="submit" class="btn btn-primary">Search</button>
{{ Form::close() }}

In Controller simple fetch $request->search

public function searchable(Request $request)
{
    // search database, with result, list on page, with links to products,
    $products = Product::search($request->search)->get();

    return view('search.index', compact('products'));
}
Sagar Chamling
  • 1,038
  • 1
  • 12
  • 26
  • Thank you, I was playing with the request object I just didnt get the right chain etc. All the best – m33bo Jul 08 '17 at 11:51