Google Captcha integration with Laravel 5.6

google captcha with laravel

In this post, I will show how to use Google ReCaptcha with laravel 5.6. First of all, you need to get Site Key and Secret Key.

Step 1: Register your site

For this, click on this link to register your site. Enter your label and site address as shown in the screenshot below:
register google captcha
After you submit register button, you will get your site key and secret key
site key and secret key

Step 2: Setup Key in your configuration file

After you get your keys, open up your .env file and add key details as like below:

Step 3: Install Captcha Package

We will use this package to use Google captcha. Install this package using composer. Run the command to install the package

Now, open your app/config/app.php and add the following.
The ServiceProvider to the providers array :

And lastly, publish configration file using command.

Step 4: Create Routes for view

Let’s create two routes for getting view file and another for posting data. I have named it as “contact”.

Step 5: Create controller

To process our application logic, we need to create a controller named ContactController. Simply run the following command to create a controller.

Lets modify our controller to to get view and process posted data from view. Final controller looks like below:

Create View

Finally, create view to display register page and captcha. I have created a default blade as a master template. default.blade.php looks like below:

So, this master template is extended by our custom contact us page. Adding form to contact template, our final contace-us.blade.php file looks like below:

That’s it. You are good to go with your captcha validation. Enjoy!