Laravel 5.8 Google ReCAPTCHA Integration

In this tutorial you will learn about the Laravel 5.8 Google ReCAPTCHA Integration and its application with practical example.

Laravel 5.8 Google ReCAPTCHA Integration

Google ReCaptcha is a one of the most popular captcha system that defends your site from bots to spam and abuse. The Google ReCaptcha is an open service that assures a computer user is a human. In order to integrate the reCaptcha, you must sign up for the Google API key & secret for your website.

In this tutorial, we will integrate google recaptcha in laravel 5.8 application. I’ll guide you through step by step how to integrate google recaptcha in a laravel 5.8 application. We will be using ‘anhskohbo/no-captcha’ package for google recaptcha integration in laravel 5.8 application. In this tutorial, we will be creating a simple form with some basic fields along with the google recaptcha.

Contents

  • Install Laravel 5.8
  • Setup Database Credentials
  • Install Google Captcha Package
  • Set Google Site Key and Secret Key
  • Define Route
  • Create Controller
  • Create Blade View (form)
  • Start Development Server
  • Conclusion

Install Laravel 5.8

First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command

Make sure you have composer installed.

Setup Database Credentials

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Install Google Captcha Package

We will be using ‘anhskohbo/no-captcha’ package for google recaptcha integration in laravel 5.8 application. In this step we will be installing “anhskohbo/no-captcha” via following composer command, lets open your terminal and switch to the project directory and enter the following command –

After Installing ‘anhskohbo/no-captcha’ package, we need to add service provider and alias in config/app.php file as following.

config/app.php

Set Google Site Key and Secret Key

Now, we need to set Google Site Key and Secret Key in .env file. Let’s start by getting Google key & secret required to make ReCaptcha authorize with the Google servers. Visit the following link to register your site –

Here, you need to provide label to identify the site. Now choose the type of reCAPTCHA as reCAPTCHA V2. Now you need to add the list of domains, this captcha would work on.

laravel_5_8_google_recaptcha_integration_2

Now accept the Terms of service and click Register. Now, you will be presented with your key and secret, add these to your application’s .env file.

Let’s open .env file and add this crendential as following –

.env

Create Controller

Now, lets create Recaptcha Controller using following command

Once the above command executed, it will create a controller file RecaptchaController.php in app/Http/Controllers/ directory. Open the RecaptchaController.php file and put the following code in it.

Controllers/RecaptchaController.php

Create View/Blade File

In this step, we will create view/blade file to render a register form with recaptcha field. Lets create a “create.blade.php” file in “resources/views/recaptcha/” directory and put the following code in it.

resources/views/recaptcha/create.blade.php

Set Routes

After this, we need to add following two routes in “routes/web.php” to display register form and to post the form data. Lets open “routes/web.php” file and add following route.

routes/web.php

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/larablog/recaptchacreate

Output:-

laravel_5_8_google_recaptcha_integration_3

In this tutorial we have learn about the Laravel 5.8 Google ReCAPTCHA Integration and its application with practical example. I hope you will like this tutorial.