PHPackages                             maize-tech/laravel-google-recaptcha-v3 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. maize-tech/laravel-google-recaptcha-v3

ActiveLibrary[Security](/categories/security)

maize-tech/laravel-google-recaptcha-v3
======================================

Laravel package for Google reCAPTCHA v3 integration with form validation and spam protection

1.0.3(6mo ago)348[3 PRs](https://github.com/maize-tech/laravel-google-recaptcha-v3/pulls)MITPHPPHP ^8.3CI passing

Since Oct 17Pushed 1mo agoCompare

[ Source](https://github.com/maize-tech/laravel-google-recaptcha-v3)[ Packagist](https://packagist.org/packages/maize-tech/laravel-google-recaptcha-v3)[ Docs](https://github.com/maize-tech/laravel-google-recaptcha-v3)[ GitHub Sponsors](https://github.com/Maize)[ RSS](/packages/maize-tech-laravel-google-recaptcha-v3/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (12)Versions (7)Used By (0)

Laravel Google reCAPTCHA v3
===========================

[](#laravel-google-recaptcha-v3)

[![Latest Version on Packagist](https://camo.githubusercontent.com/c8b699b8a16cef17fd69c10026a714ee4aa52ae3c80861d78aca0d01e867299e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61697a652d746563682f6c61726176656c2d676f6f676c652d7265636170746368612d76332e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-google-recaptcha-v3)[![GitHub Tests Action Status](https://camo.githubusercontent.com/dd066e648c109ac26ee0c5af573ceecf9e1658804d6453a4430fe007c7db88d4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d676f6f676c652d7265636170746368612d76332f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-google-recaptcha-v3/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/3040942c0c20f608273d036c7124a22e72bbcad2f21d64984242d52eacd31922/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d61697a652d746563682f6c61726176656c2d676f6f676c652d7265636170746368612d76332f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/maize-tech/laravel-google-recaptcha-v3/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/f568df9e66f6e5be8a5aad2ff26e99de32cf4019f0be7bd1669a751039f0d4a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d61697a652d746563682f6c61726176656c2d676f6f676c652d7265636170746368612d76332e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maize-tech/laravel-google-recaptcha-v3)

A Laravel package that provides a simple and elegant integration of Google reCAPTCHA v3 for your Laravel applications. This package includes a Blade directive for easy frontend integration and a validation rule for backend verification with customizable score thresholds.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require maize-tech/laravel-google-recaptcha-v3
```

You can install and configure the package with:

```
php artisan google-recaptcha-v3:install
```

This command will publish the configuration file.

This is the contents of the published config file:

```
return [
    /*
    |--------------------------------------------------------------------------
    | Enabled
    |--------------------------------------------------------------------------
    |
    | This option controls whether Google reCAPTCHA v3 is enabled.
    | When disabled, the validation will be skipped.
    |
    */
    'enabled' => env('GOOGLE_RECAPTCHA_V3_ENABLED', true),

    /*
    |--------------------------------------------------------------------------
    | Site Key
    |--------------------------------------------------------------------------
    |
    | Your Google reCAPTCHA v3 site key.
    | You can get this from https://www.google.com/recaptcha/admin
    |
    */
    'site_key' => env('GOOGLE_RECAPTCHA_V3_SITE_KEY'),

    /*
    |--------------------------------------------------------------------------
    | Secret Key
    |--------------------------------------------------------------------------
    |
    | Your Google reCAPTCHA v3 secret key.
    | You can get this from https://www.google.com/recaptcha/admin
    |
    */
    'secret_key' => env('GOOGLE_RECAPTCHA_V3_SECRET_KEY'),

    /*
    |--------------------------------------------------------------------------
    | Score Threshold
    |--------------------------------------------------------------------------
    |
    | The minimum score threshold for the reCAPTCHA validation.
    | Google reCAPTCHA v3 returns a score (1.0 is very likely a good interaction,
    | 0.0 is very likely a bot). Default is 0.5.
    |
    */
    'score_threshold' => env('GOOGLE_RECAPTCHA_V3_SCORE_THRESHOLD', 0.5),

    /*
    |--------------------------------------------------------------------------
    | Badge
    |--------------------------------------------------------------------------
    |
    | The default badge position for the reCAPTCHA badge.
    | Available options: 'bottomright', 'bottomleft', 'inline', 'hidden'.
    | Default is 'bottomright'.
    |
    */
    'badge' => env('GOOGLE_RECAPTCHA_V3_BADGE', 'bottomright'),
];
```

Configuration
-------------

[](#configuration)

After installing the package, add the following environment variables to your `.env` file:

```
GOOGLE_RECAPTCHA_V3_ENABLED=true
GOOGLE_RECAPTCHA_V3_SITE_KEY=your-site-key-here
GOOGLE_RECAPTCHA_V3_SECRET_KEY=your-secret-key-here
GOOGLE_RECAPTCHA_V3_SCORE_THRESHOLD=0.5
GOOGLE_RECAPTCHA_V3_BADGE=bottomright
```

You can obtain your site key and secret key from the [Google reCAPTCHA Admin Console](https://www.google.com/recaptcha/admin).

Usage
-----

[](#usage)

### Frontend Integration

[](#frontend-integration)

Add the reCAPTCHA script to your Blade templates using the `@recaptcha` directive:

```
@recaptcha
```

You can also customize the badge position by passing one of the available badge positions:

```

    My Form

        @csrf

        Submit

    @recaptcha(\Maize\GoogleRecaptchaV3\Enums\Badge::BOTTOMRIGHT)

        document.getElementById('myForm').addEventListener('submit', async function(e) {
            e.preventDefault();

            const token = await window.recaptcha('submit');
            document.getElementById('g-recaptcha-response').value = token;

            this.submit();
        });

```

#### Available Badge Positions

[](#available-badge-positions)

- `Badge::INLINE` - Displays the badge inline
- `Badge::BOTTOMLEFT` - Displays the badge at the bottom left
- `Badge::BOTTOMRIGHT` - Displays the badge at the bottom right (recommended)
- `Badge::HIDDEN` - Hides the badge completely

**Important Note about Hidden Badge:**When using `Badge::HIDDEN`, you must display the reCAPTCHA branding visibly in your user flow. According to [Google's guidelines](https://developers.google.com/recaptcha/docs/faq#id-like-to-hide-the-recaptcha-badge.-what-is-allowed), you need to include the following text:

```
This site is protected by reCAPTCHA and the Google
Privacy Policy and
Terms of Service apply.
```

### Backend Validation

[](#backend-validation)

Use the `googleRecaptchaV3` validation rule in your form requests or controllers:

```
use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
    'name' => 'required|string',
    'email' => 'required|email',
    'g-recaptcha-response' => ['required', 'string', Rule::googleRecaptchaV3()],
]);
```

#### Custom Score Threshold

[](#custom-score-threshold)

You can override the default score threshold (configured in `config/google-recaptcha-v3.php`) by passing a custom threshold to the validation rule:

```
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

$validator = Validator::make($request->all(), [
    'g-recaptcha-response' => ['required', 'string', Rule::googleRecaptchaV3(0.7)],
]);
```

A higher threshold (e.g., 0.7 or 0.8) means stricter validation, while a lower threshold (e.g., 0.3 or 0.4) is more permissive.

### Form Request Example

[](#form-request-example)

```
