Reputation: 2505
I was trying to implement search with laravel where i want to submit the form with post method and trying to show the query like this .
abc.com/search/q=res
Here is my routes :
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'));
}
But the problem is it shows the following Error
Object of class Symfony\Component\HttpFoundation\ParameterBag could not be converted to string
How do i implement search where in URL in shows in this way?
Upvotes: 1
Views: 1158
Reputation: 393
Define your route with Route::get('search','ProductController@searchProduct');
In the function use $params = Input::get('q');
to get a variable.
Example to request 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'));
}
Or define your route Route::get('search/{search}','ProductController@searchProduct');
In the function use public function searchProduct($search)
to get the variable $search
Example to request 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'));
}
Upvotes: 1