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

lwvvliet's avatar

Url Route parameter translation

I have this route

Route::get('/order/{producttype}', 'OrderController@product')->name('order.product');

where parameter producttype can have a value like 'type-123'. This would make the url: /order/type-123 I would like a url that is more human readable. Instead of 'type-123' something like for example 'electric-bike'. This url /order/electric-bike should route to route('order.product') with producttype has value type-123

Is this possible? How can I proceed?

0 likes
6 replies
Cronix's avatar

The easiest way to do that is to add a slug column (or name how you want) to the products table.

This url /order/electric-bike should route to route('order.product') with producttype has value type-123

That part won't work, but you can do it a different way, if you use a slug column. Then you retrieve the product by the slug.

So a route like

Route::get('/order/{slug}', 'OrderController@product')->name('order.product');

In the controller you'd just

public function product(Request $request, $slug)
{
    $product = Product::where('slug', $slug)->firstOrFail();
}

Or you can have it retrieve the model automatically using route model binding, and just tell it the slug column is the key to retrieve the record by instead of id. This is easier because then it will just automatically retrieve the model without having to manually $product = Product::where('slug', $slug)->firstOrFail();

https://laravel.com/docs/5.6/routing#route-model-binding

lwvvliet's avatar

There are a few complications which I probably didn't make clear in the initial question.

First, the 'electric-bike' part or slug as you call it can have multiple translations for different languages.

  • electric-bike
  • velo-electrique
  • elektrische-fiets should all route to the same action with parameter producttype is 'type-123'.

Second is that I don't have a Product model and can't add a slug column like you suggested.

I should probably translate possible values somewhere. Maybe in the Controller like you suggested with the slug. Could something like this also be done via Middleware or Router parameter patterns?

cmdobueno's avatar
Level 18

You could do something like this, which might do what you want... this is totally untested and just a theory though...

public function boot()
{
    parent::boot();

    Route::bind('product_slug', function ($value) {
        //Do Some Translating here...

                //Handle Response
        if( $translate_found_a_product ){
            return $my_product_that_I_got_from_translation;
        } else {
            return abort(404);
        }
     });
}

lwvvliet's avatar

Ok, I will try this approach with the translating in the RouteServiceProvider. Thanks for your message.

Please or to participate in this conversation.