Google Recaptcha V3 Example Tutorial in Laravel 8

Hello buddies,

How are you guys? Today I am going to show you how to add google Recaptcha to your laravel web application. Let’s look at an example of integrating Recaptcha in laravel application. This article will go over how to implement google Recaptcha in Laravel 8. I’ll teach you how to use Google’s v3 ReCAPTCHA with Laravel 8 forms in this article. This tutorial will show you how to use Recaptcha in laravel.

Create Laravel project

composer create-project --prefer-dist laravel/laravel blog

Set Database Configuration properties

Go to the .env file and set your Database name, Database username, and password.


Install Laravel  josiasmontag/laravel-recaptchav3

We need josiasmontag/laravel-recaptchav3 package. you can install the google v3 Recaptcha validation package in laravel 8 google v3 Recaptcha validation app. So let’s open the terminal and run the below command:

composer require josiasmontag/laravel-recaptchav3 
Github-link: josiasmontag/laravel-recaptchav3

After installing the above package, you can add providers and aliases in the config file. So let’s run the below command to publish the config file:

php artisan vendor:publish --provider="Lunaweb\RecaptchaV3\Providers\RecaptchaV3ServiceProvider"

Then add the site key and secret key on the .env file provided by the google Recaptcha website.


Create Model and Migration

We are going to create a Google Recaptcha v3 application for the contact-us page in my laravel app. So run the below command:

php artisan make:model ContactUs -m

Add the following code to your database/migrations file


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateContactUsTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('contact_us', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()


php artisan migrate

Now you can add all table columns in fillable So let’s open the model file and put the below code.



namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ContactUs extends Model
    use HasFactory;
    protected $table = 'contact_us';
    protected $fillable = ['name', 'email','subject','message'];

Create Routes

Read Also: Laravel 8 REST API Authentication with Sanctum

Go to the .env file and create the following routes respectively


Route::get('contact-us', [GoogleV3CaptchaController::class, 'index']);
Route::post('validate-g-recaptcha', [GoogleV3CaptchaController::class, 'validateGCaptch']);

Create Controller

Now it’s time to create our controller, run the below command:

php artisan make:controller recaptcha\GoogleV3CaptchaController



namespace App\Http\Controllers\recaptcha;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ContactUs;
use Validator;
use Session;

class GoogleV3CaptchaController extends Controller
    public function index()
        return view('contact');
    public function validateGCaptch(Request $request)
        $input = $request->all();

        $validator = Validator::make($input,[
            'name' => 'required',
            'email' => 'required',
            'subject' => 'required',
            'message' => 'required',
            'g-recaptcha-response' => 'required',

        if ($validator->passes()){
            return redirect('contact-us')->with('status', 'Google V3 Recaptcha has been validated form');

        return redirect()->back()->withErrors($validator)->withInput();

Create Blade File(contact)

We need to add a blade view so first create a new file contact.blade.php file and put the below code:


<!DOCTYPE html>
    <title>Google Recaptcha Validation In Laravel 8 Form</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="">
    <div class="container mt-4">
            <div class="alert alert-success">
                {{ session('status') }}
        <div class="card">
            <div class="card-header text-center font-weight-bold">
                <h2>Contact Us</h2>
            <div class="card-body">
                <form name="g-v3-recaptcha-contact-us" id="g-v3-recaptcha-contact-us" method="post" action="{{url('validate-g-recaptcha')}}">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Name</label>
                        <input type="text" id="name" name="name" class="@error('name') is-invalid @enderror form-control">
                            <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Email</label>
                        <input type="email" id="email" name="email" class="@error('email') is-invalid @enderror form-control">
                            <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Subject</label>
                        <input type="text" id="subject" name="subject" class="@error('subject') is-invalid @enderror form-control">
                            <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Message</label>
                        <textarea name="message" class="@error('description') is-invalid @enderror form-control"></textarea>
                            <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
                    <div class="form-group">
                        <input type="hidden" name="g-recaptcha-response" id="recaptcha">
                    <button type="submit" class="btn btn-primary">Submit</button>
    <script src="{{ config('services.recaptcha.sitekey') }}"></script>
             grecaptcha.ready(function() {
                 grecaptcha.execute('{{ config('services.recaptcha.sitekey') }}', {action: 'contact'}).then(function(token) {
                    if (token) {
                      document.getElementById('recaptcha').value = token;
    <!-- <script src="{{ config('services.recaptcha.sitekey') }}"></script> -->

Now we are ready to run our google Recaptcha v3 example with laravel 8 so run the below command for the quick run:

php artisan serve

Now you can open the below URL on your browser:


It will help you…