sushantaryal/laravel-recaptcha

A Laravel package to validate google recaptcha v3

1.0.1 2025-07-23 06:18 UTC

This package is auto-updated.

Last update: 2025-07-23 06:19:48 UTC


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()],
]);

License

MIT license.