Laravel 8 Google ReCAPTCHA V2 Tutorial Example




Laravel 8 google v2 Recaptcha validation in forms. In this example, you will learn how to protects web applications from spammers using google v2 recaptcha  form validation. It validates the data of your web application form. Only after this, other activities can be done.

If a user has been spammed in your web application contact form, etc. So you can stop the spammer using the google v2 Recaptcha Package.

In this laravel tutorial, you will learn how to implement or integrate google v2 recaptch with form in laravel web applications.

Laravel 8 Google Recaptcha v2 Example

  • Install Laravel Application
  • Configure Database Detail
  • Run Migration
  • Install Google Captcha Package
  • Get Google ReCaptcha Secret
  • Create Routes
  • Generate Controller By Artisan
  • Create Blade View
  • Start Development Server

Install Laravel Application

First Step, you need to download or install laravel web application new setup on your local machine. you can use the following command to download or install laravel application new setup:

composer create-project laravel/laravel laravel-google-recaptcha –-prefer-dist

If you want to install a specific version of laravel, you can follow the following tutorial

Configure Database Detail

After that, you will set up the database credentials in .env file. So open your .env file and update database credentials following:


Run Migration

Next, go to command prompt and run the following command:

php artisan migrate


php artisan migrate:fresh

This php artisan migrate command creates a table in your database.

Install Google Captcha Package

Next, we need to install the laravel google Recaptcha package. So use the following command to install the laravel google Recaptcha package.

composer require anhskohbo/no-captcha

After that, you need to register laravel Google Captcha Packages. So open config/app.php file and add service provider and alias.

'providers' => [
 'aliases' => [
 'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,

Get Google ReCaptcha Secret

Next, you need to get google Recaptcha site_key and secret_key from google Recaptcha site. If you have already site_key and secret_key, so you need to put the .env file. If you don't know where we will get secret_key and secret_site click below the link and create your own secret credentials.  Recaptcha new site registration

After that, we will set the google captcha secret in .env files, let's open .env file and add this credentials here


Create Routes

Next step, go to routes/web.php file and update the following routes into your file:

use App\Http\Controllers\GoogleReCaptchaController;

Route::get('form', [GoogleReCaptchaController::class, 'index']);
Route::post('store', [GoogleReCaptchaController::class, 'store']);

Generate Controller By Artisan

Now, you need to generate or create a controller file using php artisan command, so use the php artisan make:controller command and generate/create a new controller file:

php artisan make:controller GoogleReCaptchaController

We will create two methods in this controller file. This method will also show  Recaptcha form and store from data into the database in laravel web applications.

After that, go to App/Http/Controllers/ directory and find the created GoogleReCaptchaController file and update the following code into your file:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator,Redirect,Response;
use App\Models\User;

class GoogleReCaptchaController extends Controller
    public function index()
       return view('recaptch-form');
    public function store(Request $request)
        'name' => 'required',
        'email' => 'required',
        'mobile_number' => 'required',
        'g-recaptcha-response' => 'required|captcha',

        dd('successfully validate');

Create Blade Views

Next, you need to create a blade views file, so go resource/views directory and create the following blade views file.

You need to include the following typehead js JavaScript library in the form below.

  <script src=""></script>

Go to resource/views/ then create a new file name search-page.blade.php and update the following code:

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="csrf-token" content="{{ csrf_token() }}">

  <title>Laravel 8 Google V2 Recatpcha Validation -</title>

  <link rel="stylesheet" href="" />

  <script src=""></script>  
   .error{ color:red; } 


<div class="container">

    @if ($message = Session::get('success'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
          <strong>{{ $message }}</strong>
    <form id="captcha-form" method="post" action="{{url('store')}}">
      <div class="form-group">
        <label for="formGroupExampleInput">Name</label>
        <input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name">
        <span class="text-danger">{{ $errors->first('name') }}</span>

      <div class="form-group">
        <label for="email">Email Id</label>
        <input type="text" name="email" class="form-control" id="email" placeholder="Please enter email id">
        <span class="text-danger">{{ $errors->first('email') }}</span>

      <div class="form-group">
        <label for="captcha">Captcha</label>
          {!! NoCaptcha::renderJs() !!}
          {!! NoCaptcha::display() !!}
        <span class="text-danger">{{ $errors->first('g-recaptcha-response') }}</span>
      <div class="form-group">
       <button type="submit" class="btn btn-success">Submit</button>


Start development server

You can use the following command artisan command to start the development server:

php artisan serve

The final step, go to your browser and type the below-given URL into your browser: