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.

1 Answer 1

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'));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was playing with the request object I just didnt get the right chain etc. All the best

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.