straylightagency / laravel-captcha
Laravel package with prepared drivers to enable captcha services like Google Recaptcha or Cloudflare Turnstile using Validator Rules.
Package info
github.com/straylightagency/laravel-captcha
pkg:composer/straylightagency/laravel-captcha
Requires
- php: >=8.3.0
- illuminate/contracts: ^11.0|^12.0|^13.0
- illuminate/http: ^11.0|^12.0|^13.0
- illuminate/support: ^11.0|^12.0|^13.0
- illuminate/translation: ^11.0|^12.0|^13.0
- illuminate/validation: ^11.0|^12.0|^13.0
This package is auto-updated.
Last update: 2026-05-09 19:09:39 UTC
README
Adds Captcha verification functionality to Laravel based on services API.
Installation
Require this package with composer.
composer require straylightagency/laravel-captcha
Laravel without auto-discovery:
If you do not use auto-discovery, add the ServiceProvider to the providers array in bootstrap/providers.php :
\Straylightagency\LaravelCaptcha\CaptchaServiceProvider::class,
Use this artisan command to publish the captcha.php file inside your config folder :
php artisan vendor:publish --tag=captcha
You must set the site key and secret key based on the service you want to use inside your .env file :
## select your drive here : fake, recaptcha, recaptcha_enterprise or turnstile CAPTCHA_DRIVER=recaptcha_enterprise ## if you use Google ReCaptcha or ReCaptcha Enterprise : GOOGLE_RECAPTCHA_SITE_KEY="FAK3S1T3K3Y21AI3C9S3VJZ-V65M1SMWLGEPX4SB" GOOGLE_RECAPTCHA_API_KEY="FAK3AP1K3YXEFG7V2EV1HK1-RGGGU817475938X1O0" GOOGLE_RECAPTCHA_PROJECT_ID="project-123456" GOOGLE_RECAPTCHA_SECRET_KEY="FAK3S3CR3TK3YBQYVMJQYMLT4W57RTFFO0MF38VB" ## if you use Cloudflare Turnstile : CLOUDFLARE_TURNSTILE_SITE_KEY="FAK3S1T3K3YNPY1AEHI2O6P-6AYWQVLJ8439LG3N" CLOUDFLARE_TURNSTILE_SECRET_KEY="FAK3S3CR3TK3Y8X1O0T0Q2KBQWLKEDUQYNP6GHE3"
In your languages directory, add an extra translation in every validation.php language file :
'captcha' => 'The captcha is not valid.',
Validation
You can use the captcha_rule() helper function directly inside the validation array :
$validator = Validator::make( $request->all(), [ // [...] 'captcha' => ['required', 'string', captcha_rule()], ] );
You can also use the captcha rule macro :
$validator = Validator::make( $request->all(), [ // [...] 'captcha' => ['required|string|captcha'], // works too ] );
Use the ->default() method if you want to use the required and string macro with the captcha rule.
$validator = Validator::make( $request->all(), [ 'name' => ['required', 'string'], 'email' => ['required', 'string', 'email'], 'content' => ['required', 'string'], ...captcha_rule()->default(), // parameter $key_name of default() method allow you to edit the field name. ] );
Some Verifier needs a script tag inside the <head> tag of your HTML page, you can use an helper function to print
the right script based on the selected Verifier :
<head>
<!-- rest of your head tag -->
{!! captcha_script() !!}
<!-- rest of your head tag -->
</head>
For more information about frontend integration, check the documentation bellow from the API service you want to use.
Requirement
PHP 8.3 or above
See also
Credits
License
The MIT License (MIT).