1

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?

0

1 Answer 1

1

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

2 Comments

when i tried with the second option, the Query doesn't work, means it showing all products data
@U Use the second form I've posted, and verify that function productSearch is getting $request->name

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.