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

Ligonsker's avatar

Google reCAPTCHA and Laravel

If I need to add reCAPTCHA to my project, how can I globally validate the user response wherever there is an input instead of checking in each request separately? Is there a way to globally check for the response wherever there is one? this is the response received by the reCAPTCHA API for reference: ( https://developers.google.com/recaptcha/docs/verify#api-response )

{
  "success": true|false,
  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
  "apk_package_name": string, // the package name of the app where the reCAPTCHA was solved
  "error-codes": [...]        // optional
}

Maybe using a Service Provider?

0 likes
6 replies
Ligonsker's avatar

@Sergiu17 and then attach the middleware to the route that the data is posted to?

Btw, what made you decide it's more suitable for a middleware and not service provider? I'm still not the most familiar with them

Sergiu17's avatar

@Ligonsker I just don't understand how you will use it in a service provider, I guess you will register your class ( service ) in a service provider, but the actual google captcha check will be done in a middleware.

Read more about Service Providers here: https://laravel.com/docs/9.x/providers

another option is to create a custom validation rule

1 like
Ligonsker's avatar

@Snapey what spam?

Also, Your package looks awesome. I might use it in the login/registration form if it's approved. But for other situations such as filling up surveys or forms, the solution does not have the invisible recaptcha feature that detects for unusual activity without interrupting the user

Please or to participate in this conversation.