PHPackages                             abcdebo/laravel-google-recaptcha - 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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. abcdebo/laravel-google-recaptcha

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

abcdebo/laravel-google-recaptcha
================================

Google Recaptcha For Laravel.

03PHP

Since Jan 30Pushed 2y ago1 watchersCompare

[ Source](https://github.com/abcdebo/laravel-google-recaptcha)[ Packagist](https://packagist.org/packages/abcdebo/laravel-google-recaptcha)[ RSS](/packages/abcdebo-laravel-google-recaptcha/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

No CAPTCHA reCAPTCHA
====================

[](#no-captcha-recaptcha)

[![Build Status](https://camo.githubusercontent.com/ec2b07726185497c4b0024ebb7cf8ec6263bdb107c3b2d9b3794501a5be15726/68747470733a2f2f7472617669732d63692e6f72672f6162636465626f2f676f6f676c652d7265636170746368612e7376673f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://travis-ci.org/abcdebo/google-recaptcha)[![Latest Stable Version](https://camo.githubusercontent.com/4786c7d40a8a4c76b34ceee015302d5840c0e3c6ba7abccb7bc052d0caeb440a/68747470733a2f2f706f7365722e707567782e6f72672f6162636465626f2f676f6f676c652d7265636170746368612f762f737461626c65)](https://packagist.org/packages/abcdebo/google-recaptcha)[![Total Downloads](https://camo.githubusercontent.com/a90a4d1e907cee76faef2d5f9bc06a45d362632b1b719da4490bc422700b59c4/68747470733a2f2f706f7365722e707567782e6f72672f6162636465626f2f676f6f676c652d7265636170746368612f646f776e6c6f616473)](https://packagist.org/packages/abcdebo/google-recaptcha)[![Latest Unstable Version](https://camo.githubusercontent.com/01da02ad272904583b8593c0c1f519933af10ae48083bd160479d5f7b1317325/68747470733a2f2f706f7365722e707567782e6f72672f6162636465626f2f676f6f676c652d7265636170746368612f762f756e737461626c65)](https://packagist.org/packages/abcdebo/google-recaptcha)[![License](https://camo.githubusercontent.com/03ab54a669545b32247f9a3530784d958f576c9f541106e116b2e870d4106843/68747470733a2f2f706f7365722e707567782e6f72672f6162636465626f2f676f6f676c652d7265636170746368612f6c6963656e7365)](https://packagist.org/packages/abcdebo/google-recaptcha)

[![recaptcha_anchor 2x](https://cloud.githubusercontent.com/assets/1529454/5291635/1c426412-7b88-11e4-8d16-46161a081ece.gif)](https://cloud.githubusercontent.com/assets/1529454/5291635/1c426412-7b88-11e4-8d16-46161a081ece.gif)

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

[](#installation)

```
composer require abcdebo/laravel-google-recaptcha

```

Laravel 5 and above
-------------------

[](#laravel-5-and-above)

### Setup

[](#setup)

***NOTE*** This package supports the auto-discovery feature of Laravel 5.5 and above, So skip these `Setup` instructions if you're using Laravel 5.5 and above.

In `app/config/app.php` add the following :

1- The ServiceProvider to the providers array :

```
ABCDebo\GoogleRecaptcha\GoogleRecaptchaServiceProvider::class,
```

2- The class alias to the aliases array :

```
'GoogleRecaptcha' => ABCDebo\GoogleRecaptcha\Facades\GoogleRecaptcha::class,
```

3- Publish the config file

```
php artisan vendor:publish --provider="ABCDebo\GoogleRecaptcha\GoogleRecaptchaServiceProvider"

```

### Configuration

[](#configuration)

Add `GoogleRecaptcha_SECRET` and `GoogleRecaptcha_SITEKEY` in **.env** file :

```
GoogleRecaptcha_SECRET=secret-key
GoogleRecaptcha_SITEKEY=site-key

```

(You can obtain them from [here](https://www.google.com/recaptcha/admin))

### Usage

[](#usage)

#### Init js source

[](#init-js-source)

With default options :

```
 {!! GoogleRecaptcha::renderJs() !!}
```

With [language support](https://developers.google.com/recaptcha/docs/language) or [onloadCallback](https://developers.google.com/recaptcha/docs/display#explicit_render) option :

```
 {!! GoogleRecaptcha::renderJs('fr', true, 'recaptchaCallback') !!}
```

#### Display reCAPTCHA

[](#display-recaptcha)

Default widget :

```
{!! GoogleRecaptcha::display() !!}
```

With [custom attributes](https://developers.google.com/recaptcha/docs/display#render_param) (theme, size, callback ...) :

```
{!! GoogleRecaptcha::display(['data-theme' => 'dark']) !!}
```

Invisible reCAPTCHA using a [submit button](https://developers.google.com/recaptcha/docs/invisible):

```
{!! GoogleRecaptcha::displaySubmit('my-form-id', 'submit now!', ['data-theme' => 'dark']) !!}
```

Notice that the id of the form is required in this method to let the autogenerated callback submit the form on a successful captcha verification.

#### Validation

[](#validation)

Add `'g-recaptcha-response' => 'required|captcha'` to rules array :

```
$validate = Validator::make(Input::all(), [
	'g-recaptcha-response' => 'required|captcha'
]);
```

##### Custom Validation Message

[](#custom-validation-message)

Add the following values to the `custom` array in the `validation` language file :

```
'custom' => [
    'g-recaptcha-response' => [
        'required' => 'Please verify that you are not a robot.',
        'captcha' => 'Captcha error! try again later or contact site admin.',
    ],
],
```

Then check for captcha errors in the `Form` :

```
@if ($errors->has('g-recaptcha-response'))

        {{ $errors->first('g-recaptcha-response') }}

@endif
```

### Testing

[](#testing)

When using the [Laravel Testing functionality](http://laravel.com/docs/5.5/testing), you will need to mock out the response for the captcha form element.

So for any form tests involving the captcha, you can do this by mocking the facade behavior:

```
// prevent validation error on captcha
GoogleRecaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// provide hidden input for your 'required' validation
GoogleRecaptcha::shouldReceive('display')
    ->zeroOrMoreTimes()
    ->andReturn('');
```

You can then test the remainder of your form as normal.

When using HTTP tests you can add the `g-recaptcha-response` to the request body for the 'required' validation:

```
// prevent validation error on captcha
GoogleRecaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// POST request, with request body including g-recaptcha-response
$response = $this->json('POST', '/register', [
    'g-recaptcha-response' => '1',
    'name' => 'John',
    'email' => 'john@example.com',
    'password' => '123456',
    'password_confirmation' => '123456',
]);
```

Without Laravel
---------------

[](#without-laravel)

Checkout example below:

```
