尝试为产品设置基本搜索功能。我在对路由参数变量进行排序并将查询字符串传递给搜索函数时遇到问题。
Route::get('/search/{query?}', 'ProductController@searchable');
这有效并在我手动输入查询时返回查询。
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'));
}
但是,我希望它来自 URL /search?test
。
我的表格显示:
{{ 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() }}`
我是 Laravel 的新手,需要一点帮助。我正在使用 Laravel Scout 和 TNTSearch。
最佳答案
您不需要使用 {wildcard}
进行搜索。我们对此有 Request
Route::get('search', 'ProductController@searchable');
改为传递 url。
{{ 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() }}
在 Controller 中简单获取$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'));
}
关于php - 将输入查询添加到搜索功能 Laravel Scout 的路由值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44977477/