sushantaryal / laravel-recaptcha
A Laravel package to validate google recaptcha v3
1.0.1
2025-07-23 06:18 UTC
Requires
- php: ^8.1
- google/recaptcha: ^1.3
- illuminate/support: ^10.0|^11.0|^12.0
- illuminate/validation: ^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^9.0|^10.4|^11.5
README
A Laravel package for validating Google Recaptcha v3.
Installation
You can install the package via Composer:
composer require sushantaryal/laravel-recaptcha
Getting reCAPTCHA keys
Follow the official documentation to get the reCAPTCHA site key and secret key.
Configuration
Add these to your .env
:
RECAPTCHA_KEY= RECAPTCHA_SECRET= RECAPTCHA_THRESHOLD=0.5
Usage
Form
Follow the official documentation to get the reCAPTCHA HTML code.
Automatically bind the challenge to a button
<script src="https://www.google.com/recaptcha/api.js"></script>
Add a callback function to handle the token.
<script> function onSubmit(token) { document.getElementById("demo-form").submit(); } </script>
Add attributes to your html button.
<button class="g-recaptcha" data-sitekey="{{ config('recaptcha.key') }}" data-callback="onSubmit" data-action="submit" > Submit </button>
OR
Programmatically invoke the challenge
Use the <x-recaptcha::input />
in your form.
<form action="" method="post" id="recaptcha-form"> @csrf <!-- Add for form inputs --> <x-recaptcha::input /> <button type="submit">Submit</button> </form>
Load the JavaScript with the recaptcha blade component before closing body tag.
<body> ... <x-recaptcha::script :formId="'recaptcha-form'" :action="'submit'" /> </body>
Backend
You can use the Recaptcha
rule in your validation rules:
use Sushant\LaravelRecaptcha\Rules\Recaptcha; $request->validate([ 'g-recaptcha-response' => ['required', new Recaptcha()], ]);