1
votes

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.

1

1 Answers

3
votes

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'));
}