1

I am using laravel and I am facing one problem, I have URL like this

https://example.com?version=2.2.0

Now I am creating middleware after matching the version the query parameter should remove. Below is the middleware code

public function handle($request, Closure $next)
    {
        $input = $request->all();
        $request->replace($request->except(['version']));
        return $next($request);
    }

But it is not working to remove query parameters although working post data.

3 Answers 3

1

Why don't use just remove method?

public function handle($request, Closure $next)
{
    $input = $request->all();
    $request->remove('version');
    return $next($request);
}

This is what remove method does under the hood, in laravel source code:

/**
 * Removes a parameter.
 */
public function remove(string $key)
{
    unset($this->parameters[$key]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

"message": "Method Illuminate\\Http\\Request::remove does not exist."
Which version of laravel are you using?
we are using laravel version 6.2
0

Just unset the query param.

public function handle($request, Closure $next)
{
    if( $request->has('version') ){
        unset($request['version']);
    }
    return $next($request);
}

1 Comment

it is not working
0

If i understand correctly you want to remove the ?version=2.2.0 from your url?

You can do this by using this code:

// This only works for GET requests, NOT for POST requests.
if ($request->has('version')) {
        return redirect()->to($request->fullUrlWithoutQuery('version'));
}

return $next($request);

3 Comments

This will not work if request is submitted by POST method
@MalkhaziDartsmelidze it can be any request
You can use the answer of @MalkhaziDartsmelidze as seen below.

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.