logo

A use case for global route constraints

I don't see this used much

Joel Clermont
Joel Clermont
2025-09-29

I got some additional reader feedback from the two possible solutions I shared for handling Postgres route model binding type issues.

One person pointed out that there is a pre-defined route constraint for integers that we can use instead of defining our own regex:

// routes/web.php
Route::get('/users/{user}', [UserController::class, 'show'])
    ->whereNumber('user');

That was a nice simplification. But it didn't really address the biggest annoyance with this approach, which is having to repeat it for each route.

Another reader offered a solution that potentially solves that problem. They mentioned Laravel's global constraints.

In your AppServiceProvider, you can register a constraint to apply to all parameters of a particular name.

// app/Providers/AppServiceProvider.php
public function boot(): void
{
    Route::pattern('user', '[0-9]+');
}

Now I don't need to do anything in my routes file, but I would either have to add a pattern for every model (user, post, and so on). Or I'd have to name the parameters something generic like {id}, which feels weird with the route model binding convention.

As I reflect on all these possible solutions, they all have some merit and some drawbacks. Honestly, though, I think the real solution is that Laravel should handle the problem for us, and not let the database differences leak through the Eloquent abstraction layer.

Here to help,

Joel

P.S. Don't leave security as an after-thought in your Laravel application. Download our free ebook: 7 Steps to a Secure Laravel App.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.