How to add Google ReCaptcha V2 in Laravel 9?

https://www.itsolutionstuff.com/post/how-to-add-google-recaptcha-v2-in-laravel-9example.html

How to add Google ReCaptcha V2 in Laravel 9?

By Hardik Savani May 17, 2022 Category : LaravelPauseUnmuteLoaded: 3.10%FullscreenVDO.AIHi Dev,

This article will provide some of the most important example laravel 9 google recaptcha v2 example. We will use how to use google recaptcha v2 in laravel 9. if you want to see example of how to add google recaptcha v2 in laravel 9 then you are a right place. you can understand a concept of integrate google recaptcha v2 in laravel 9.

As we know, When we created a contact us form or feedback form or any form that is accessed publicly Then we have so many submissions from spam. They submit those forms using curl request or something. So we can not get the real users from those forms. Here, i we will use google recaptcha v2 to prevent spam submission. we will create a contact us form and use google recaptcha v2 and prevent them. So that way you have only real users and real feedback you will get.

Let's see below steps and make it done.

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Add Google API Key

In this step we need to set google site key and secret key. If you don't have site key and secret key then you can create from here. First click on this link : Recaptcha Admin

Ok, after sucessfully register you can get site key and secret key from like bellow preview.

Now open .env file and add this two variable

.env

Read Also: Laravel 9 REST API Authentication using Sanctum Tutorial

Step 3: Create Routes

In this step, we will create two routes GET and POST. so let's add it.

routes/web.php

Step 4: Create Validation Rule

In this step, we will create new "ReCaptcha" validation rule that will check user is real or not using google recaptcha v2 API. so let's run below command and update rule validation file.

app/Rules/ReCaptcha.php

Step 5: Create Controller

In this step, we have to create new controller as ContactController and we have also need to add two methods index() and store() on that controller. We will use recaptcha validation on store method. so let's update follow code:

app/Http/Controllers/ContactController.php

Step 6: Create View File

In Last step, let's create contactForm.blade.php(resources/views/contactForm.blade.php) for create form with google recaptcha v2 and put following code:

resources/views/contactForm.blade.php

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

Now, Go to your web browser, type the given URL and view the app output:

Read Also: How to Send an Email on Error Exceptions in Laravel 9?

Output: You Email Look Like This

I hope it can help you...

Last updated

Was this helpful?