Laravel 9 Google Recaptcha V3 Tutorial with Example

In this tutorial you will learn about the Laravel 9 Google Recaptcha V3 Tutorial with Example and its application with practical example.

In this Laravel 9 Google Recaptcha V3 Tutorial with Example I will show you how to integrate google recaptcha v3 in laravel 9 application. I’ll guide you through step by step for integration of google recaptcha v3 in a laravel 9 application. We will be using ‘josiasmontag/laravel-recaptchav3’ package for laravel 9 google recaptcha v3 integration in laravel 9 application. In this tutorial, we will be creating a simple form with some basic fields along with the google recaptcha v3.

Laravel 9 Google Recaptcha V3 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.

Laravel 9 Google Recaptcha V3 Tutorial with Example

  • Install Laravel 8
  • Setup Database Credentials
  • Install Laravel Google Recaptcha V3 Package
  • Set Google Site Key and Secret Key
  • Define Route
  • Create Controller
  • Create Blade View
  • Start Development Server
  • Conclusion

Install Laravel 9

First of all we need to create a fresh laravel project, download and install Laravel 9 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 Laravel Google Recaptcha V3 Package

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

After successfully installing package, we need to publish config file with bellow command:

Use the above command

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 V3. Now you need to add the list of domains, this captcha would work on.

Laravel 9 Google Recaptcha V3 Tutorial with Example

Laravel 9 Google Recaptcha V3 Tutorial with Example 2

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

.env

Set the details as above

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 “index.blade.php” file in “resources/views/recaptcha/” directory and put the following code in it.

resources/views/recaptcha/index.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

Start Development Server

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 –

In this tutorial we have learn about the Laravel 9 Google Recaptcha V3 Tutorial with Example and its application with practical example. I hope you will like this tutorial.