我试图用laravel实现搜索,我想用post方法提交表单并尝试显示这样的查询。
abc.com/search/q=res
这是我的路线:
Route::post('productSearch','ProductController@productSearch');
Route::get('search/q={query}','ProductController@searchProduct');
public function productSearch(Request $request)
{
$query = $request->name;
return redirect('search/q='.$query);
}
public function searchProduct(Request $request)
{
$name = $request->query;
$products = DB::table('products')
->select('products.*')
->where('products.name' , 'like', '%'.$name.'%') //Error in this line
return view('product',compact('products'));
}
但问题是它显示以下错误
Object of class Symfony\Component\HttpFoundation\ParameterBag could not be converted to string
如何以这种方式在节目中的URL中实现搜索?
答案 0 :(得分:1)
使用Route::get('search','ProductController@searchProduct');
在函数中使用$params = Input::get('q');
来获取变量。
请求search?q=name
public function searchProduct(Request $request)
{
$name = Input::get('q');
// or this option
//$name = $request->input('q');
$products = DB::table('products')
->select('products.*')
->where('products.name' , 'like', '%'.$name.'%') //Error in this line
return view('product',compact('products'));
}
或定义您的路线Route::get('search/{search}','ProductController@searchProduct');
在函数中使用public function searchProduct($search)
来获取变量$ search
请求abc.com/search/xxxxxxxx
public function productSearch(Request $request)
{
$query = $request->name;
return redirect('search/'.$query);
}
public function searchProduct($search)
{
$products = DB::table('products')
->select('products.*')
->where('products.name' , 'like', '%'.$search.'%');
return view('product',compact('products'));
}