Use validation. https://laravel.com/docs/12.x/validation#main-content
Mar 19, 2025
3
Level 1
Route::apiResource() "whereNumber()" Not Working in Laravel
Question:
Hi everyone,
I'm using Laravel and have defined an API resource route like this:
Route::apiResource('news', 'NewsController')->whereNumber('news');
However, the whereNumber('news') does not seem to work. If someone accesses the route with a non-numeric parameter, Laravel still tries to match it, causing an SQL error because the parameter is not a valid number.
I want to ensure that the news parameter must be a number, so invalid requests won't trigger SQL errors.
Is there a proper way to enforce this validation in routes?
Thanks in advance! 🙏
Level 104
@triadi I am pretty sure it should be working; you didn't cache your routes, did you? If yes, then clear them
php artisan route::clear
2 likes
Please or to participate in this conversation.