Laravel Router Draws in URL Encoding from View, Thus URL Encoding Model Attribute

Original route went to /{id}, and I changed the route to /{name}

In the view, I iterate over the particular model, leading to this blade html:

<a href="{{ route('modelHistory', urlencode(strtolower($model->name)) ) }}">
                                  {{$model->name}}
                                </a>

And then I had some code in the Controller to reconvert the name passed to the route into a name that matches what’s in the database.

Laravel, somehow, sucked the urlencode and strtolower code into itself, possibly the middleware, such that any retrieval of Model::all gave us a name that was urlencoded and all lowercase.

It became an issue, because I wanted to change URL to be the name converted to lowercase with underscores instead of urlencoded, but the names remained urlencoded and lowercased, which was a problem when another view lists all instances of Model and surfaces the name attribute.

Solution below.


Source: stackoverflow-php