2017-08-26 13 views
1

私はlaravelで検索を実装しようとしていましたが、postメソッドでフォームを送信し、このようなクエリを表示しようとしました。ここでLaravelで検索を実装する

abc.com/search/q=res 

は私のルートです:

Route::post('productSearch','[email protected]'); 
    Route::get('search/q={query}','[email protected]'); 

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で検索を実装してください、次のエラーを示しているのですか?

答えて

1

は、変数を取得するには、関数の使用$params = Input::get('q');Route::get('search','[email protected]');

であなたのルートを定義します。 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')); 
    } 

場合や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')); 
    } 
+0

を要求するために変数$検索に

例を取得するには、関数の使用public function searchProduct($search)では、あなたのルートRoute::get('search/{search}','[email protected]');

を定義を要求する

例私は2番目のオプション、クエリが機能しない、すべての商品データを表示することを意味します – User57

+0

@U私が投稿した2番目のフォームを使用し、productSearch関数が$ request-> name –