Using Google ReCaptcha v3 in Laravel

Praveen K
2 min readNov 21, 2020

Introduction

Google’s recaptcha is an effective way to curb the spammers in your web forms. It helps differentiate the bots from humans using a scoring system. Let me explain how I use it in my Laravel apps on pages that has a public web form.

Step 1: Getting the keys

  1. Open Google recaptcha admin console : Link
  2. Create a new site entering the following details : Enter a site-name, select recaptcha v3, and enter the domains you wish to access this recaptcha and save. This will give you the Recaptch key and secret.
  3. Open .env file and paste the following

replacing the key and secret with your own.

4. Open config/app.php and paste the following

Step 2: Setting up your frontend

Open the partial file or the view in which your form exists and paste the following code just before the form element.

Step 3: The controller setup

Before we update the contoller let’s create a new validation rule for the recaptcha. Use php artisan make:rule Recaptcha to create a new rule and paste the following code in the file

And, finally in the controller when validating the submitted form, use the recaptcha_response field name and the recaptchavalidation rule, like

This way you can easily use the Google’s ReCaptcha in any Laravel form.

--

--

Praveen K

A freelance web developer testing the waters on becoming a keyboard warrior.