Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abdullaf's avatar

Laravel optional multiple params routes

Hi, I just wondered if is possible to make a route like this Route::get('/products/{id?}&{brand?}') ??

For example: I need to be in URL localhost/products/1&nike

0 likes
30 replies
gych's avatar

Yes this is possible but if you want to follow the recommended conventions you should use / instead of &

abdullaf's avatar

But if I use / it will be like this: localhost/products/1/nike

But I need to be like this: localhost/products/1&nike

gych's avatar

@abdullaf If you really need the url to be: localhost/products/1&nike You can still use it that way, it will work with: Route::get('/products/{id?}&{brand?}')

abdullaf's avatar

@gych When I go to route /products without params. This error appears: The GET method is not supported for route contact-us. Supported methods: POST.

gych's avatar

@abdullaf You have to create two routes in your routes file One for /products/{id?}&{brands?} and one for /products

Because if you only use this route /products/{id?}&{brands?} It will expect you to use /products/& if you use it without parameters

If they use the same controller also make sure the default value for the params is null, for example:

public function index($id = null, $brand = null)
{
// REST OF YOUR CODE
}
gych's avatar

@abdullaf Ok good and did you add two routes in your routes file One for /products/{id?}&{brands?} and one for /products

Route::get('/products/{id?}&{brand?}')

Route::get('/products}')

abdullaf's avatar

@gych Thank you! But the same issue, appear in URL like this: localhost/products/& when I didn't provide params

gych's avatar

@abdullaf So a url like localhost/products and localhost/products/1&nike work fine now? But you still get an error when you use localhost/products/& , right?

Is it the same error as before?

abdullaf's avatar

@gych No when I go just to URL: localhost/products show me this URL: localhost/products&

abdullaf's avatar

@gych

public function products($id = null, $brand= null)
    {
        return view('products.index', [
            'id' => $id,
            'brand' => $brand,
        ]);
    }
gych's avatar

@abdullaf strange so when you enter localhost/products in your adress bar it auto redirects you to localhost/products& ?

abdullaf's avatar

@gych Maybe this is the problem from the view because the second parameter is not coming from the blade it will randomly generated and will be converted to a QR Code and the URL should be like this: localhost/products/nike

gych's avatar

@abdullaf Yes but it might be possible that when there are not parameters you view creates a url like this localhost/products& without the parameters

You should use a statement in your view that when there are no paramters it doesnt add the '&' to the URL But I'm not sure about this without seeing your view file.

fulufhelo's avatar

@abdullaf how i would typically do it is Route::get('/products/{id}')->name('products.brand')

and use it as route('products.brand', ['brand' => 'nike'])

it should give you /products/1?brand=nike

abdullaf's avatar

@fulufhelo Thank you for your answer, but this one requires a link/button or anything else to implement but I don't need to be in the app because this URL will be used for QR Code just to scan, and I will generate random URLs.

martinbean's avatar

@abdullaf I think you need to familiarise yourself with “resource” controllers and routes. You shouldn’t have a route with a nullable “id” parameter, you should instead have two separates routes: an index route for listing products, and then a separate show route for viewing the details of an individual product by ID.

For the index route, you can then use query string parameters—which Laravel supports out of the box with no special handling—to filter and sort records. You don’t need to encode these in a route definition; you just access them using $request->query('brand') etc.

abdullaf's avatar

@martinbean Thank you, for your answer I know the "resource", but for me is a bit different because the second parameter will be used to track how many views will come from that specific URL it will be a QR Code but the first parameter is when a user click the button that will go to the details page. Now I need 2 optional parameters when can I use them for different purposes

martinbean's avatar
Level 80

@abdullaf You have an ID in the URI. That suggests you have an “index” route and a “show” route. I don’t really see what tracking has to do with anything.

Route::get('/products', [ProductController::class, 'index']);
Route::get('/products/{id}', [ProductController::class, 'show']);

You can then use a query string on your index view to filter results, i.e. /products?brand=nike to only show Nike-branded products in the listing:

public function index(Request $request)
{
    $products = Product::query()
        ->when($request->query('brand'), function (Builder $query, string $brand) {
            $query->where('brand', '=', $brand);
        })
        ->paginate();

    return view('products.index', compact('products'));
}

public function show(Product $product)
{
    return view('products.show', compact('product'));
}

There’s no need to try and shoehorn multiple responsibilities into a single controller action.

Please or to participate in this conversation.