Yes you can!
Documentation: https://laravel.com/docs/5.6/routing#parameters-regular-expression-constraints
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In a site my workplace just released to replace an old on, we use Type-Hinting on some controllers.
For example:
public function category(Category $category)
Where the request is something like /category/$id. So the type hinting will make $category of the Category class when it finds it based on $id.
However, if a user puts something in like /category/oldsite.php then the database lookup will fail with an error like: Conversion failed when converting the nvarchar value 'oldsite.php' to data type int. (SQL: select top 1 * from [Categories] where [Category_ID] = oldsite.php
Aside from re-writing all the Type-Hinting to just be regular variables, then validating and setting the object myself, is there anything I can put on the controller to ensure the $id parameter is an integer? And then possibly throw a 404 instead of 500?
Yes you can!
Documentation: https://laravel.com/docs/5.6/routing#parameters-regular-expression-constraints
Please or to participate in this conversation.