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

triadi's avatar
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! 🙏

0 likes
3 replies
tykus's avatar
tykus
Best Answer
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
triadi's avatar
Level 1

@tykus thank you tykus, it works. I forgot to clear my cache

Please or to participate in this conversation.