skip to Main Content

I have a route

Route::get('/edit/{id}', 'EditController')->name('edit');

I have a route param named id, but I also need query param id, example:

/edit/1?id=9999

When I use

route('edit', ['id' => 1])

Only route param is passed

/edit/1

Since query param name can not change, I would like to ask if there is any other way other than

  • Change the route param
Route::get('/edit/{someOtherName}', 'EditController')->name('edit');
  • Concat query string
route('edit', ['id' => 1]) . "?" . http_build_query(['id' => 9999])

2

Answers


  1. Yes, you would need to either change the route key name or manually append the query string to the generated url.

    The route helper’s signature is

    // Illuminate/Foundation/helpers.php
    function route($name, $parameters = [], $absolute = true)
    

    It does not have a way to specify query parameters and route parameters separately. The $parameters array accounts for both.

    Eventually, the route() helper will call the IlluminateRoutingRouteUrlGenerator class’s to method which has a very similar signature

    // Illuminate/Routing/RouteUrlGenerator.php
    public function to($route, $parameters = [], $absolute = false)
    {
        $domain = $this->getRouteDomain($route, $parameters);
    
        // First we will construct the entire URI including the root and query string. Once it
        // has been constructed, we'll make sure we don't have any missing parameters or we
        // will need to throw the exception to let the developers know one was not given.
        $uri = $this->addQueryString($this->url->format(
            $root = $this->replaceRootParameters($route, $domain, $parameters),
            $this->replaceRouteParameters($route->uri(), $parameters),
            $route
        ), $parameters);
    
        if (preg_match_all('/{(.*?)}/', $uri, $matchedMissingParameters)) {
            throw UrlGenerationException::forMissingParameters($route, $matchedMissingParameters[1]);
        }
    
        // Once we have ensured that there are no missing parameters in the URI we will encode
        // the URI and prepare it for returning to the developer. If the URI is supposed to
        // be absolute, we will return it as-is. Otherwise we will remove the URL's root.
        $uri = strtr(rawurlencode($uri), $this->dontEncode);
    
        if (! $absolute) {
            $uri = preg_replace('#^(//|[^/?])+#', '', $uri);
    
            if ($base = $this->request->getBaseUrl()) {
                $uri = preg_replace('#^'.$base.'#i', '', $uri);
            }
    
            return '/'.ltrim($uri, '/');
        }
    
        return $uri;
    }
    

    There, the route keys are replaced from the $parameters array and what is left over gets added to the query string.

    Since PHP does not support having duplicate keys in it’s associative arrays, you are stuck with the two solutions you came up with.

    Login or Signup to reply.
  2. You should try this

    url('/edit/'.1.'?id='.9999)
    

    And in controller you can get id parameter by using this

    request()->query('id')
    

    It will return 9999, and for getting 1 you can simply pass it as like this

    public function edit($id){
       // it will return 1
       dd($id);
      // it will return 9999 
      request()->query('id')
      
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search