skip to Main Content

I have two methods in my controller.

Method Number one

public function search(Request $request)
{
    $search_text = $_GET['search'];
}

This method is getting me the value entered in the search bar input via $search_text variable.

My goal here is to use that variable in another method.

Method Number Two

public function searchFilter(Request $request)
{
    $brand = $request->marque;
    $disponible = $request->disponibilite;
    $souscat = $request->sous_categorie;
}

I have tried using the session and __construct but didn’t work.

2

Answers


  1. Chosen as BEST ANSWER

    I figure it out I used session

    My First Method:
    
    public function search(Request $request)
    {
    $search_text = $_GET['search'];
    session()->put('search_text', $search_text);
    }
    

    My Second Method

    public function searchFilter(Request $request)
    {
        $search_text = session()->get('search_text');
        dd($search_text);
    }
    

    // OUTPUT

    what ever The searched value was 
    

  2. I don’t know why you need two methods for that, I think that you’re calling searchFilter as a post request or something like that, and in your query string you have the search attribute, so you could use $request to get all values in your url, you just need to call the actual attribute that comes from the route parameters: https://laravel.com/docs/10.x/requests#retrieving-input-via-dynamic-properties

    Or even you could use this method $request->query('search') to get the value for that attribute.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search