How to integrate Google recaptcha in Laravel 12

 Add Google reCAPTCHA in Laravel Forms

How to integrate Google recaptcha in Laravel 12

Adding Google reCAPTCHA  to your form form add a major security level for your application. In this article we will see a detailed overview of what is reCAPTCHA  and why its useful and how to integrate google reCAPTCHA  in Laravel.

🔍 What is Google reCAPTCHA ?

Google reCAPTCHA is a free security service that protects your website from spam and abuse. It uses advanced risk analysis techniques to tell humans and bots apart. It provides different types or version of captcha like v2 and v3. It validates that the request coming to your application from valid users.

There are different versions:

✅ reCAPTCHA v2 – “I’m not a robot” checkbox

🔄 reCAPTCHA v2 Invisible – no user interaction needed until its suspicious

🔍 reCAPTCHA v3 – works silently in the background

🛑 Why reCAPTCHA is Important?

Web forms are often targeted by bots that:

🚀 Spam your email or database

📬 Flood your inbox with fake entries

💰 Exploit your services (like signups, login, comments)

🔐 Adding reCAPTCHA helps:

  • Protect against bots 🤖
  • Improve form security 🔐
  • Maintain data integrity 📊
  • Prevent server overloads 🖥️
Now let's see step by step how to integrate Google reCAPTCHA in your Laravel Forms. Here i have created a simple user register form and added reCAPTCHA in the register form. On submission i am validating whether the captcha is submitted or not if submitted then only store the user data.

📌 Step 1: Get reCAPTCHA Keys from Google:

Go to Google reCAPTCHA admin panel.

Register your site:
  • Label: Any name (e.g., "Laravel Form")
  • reCAPTCHA type: Select reCAPTCHA v2 ("I'm not a robot")
  • Domains: Add your domain (or use localhost for testing)
  • After submission you will see Site key and Secret key.
Copy the Site Key and Secret Key.

How to integrate Google recaptcha in Laravel 12


How to integrate Google recaptcha in Laravel 12

📌 Step 2: Add Keys to .env

GOOGLE_RECAPTCHA_SITE_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
GOOGLE_RECAPTCHA_SECRET_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

📌 Step 3: Add reCAPTCHA to Blade Form (register.blade.php)

Following is the code of blade file, where i have created a simple user register form with google reCAPTCHA.

<!DOCTYPE html>
<html>
<head>
    <title>User Registration</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body class="bg-light">

<div class="container d-flex justify-content-center align-items-center" style="height: 100vh;">
    <div class="card p-4 shadow" style="min-width: 400px;">
        <h4 class="text-center mb-3">Register</h4>

        @if ($errors->any())
            <div class="alert alert-danger">
                <ul class="mb-0">
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form action="{{ route('register.store') }}" method="POST">
            @csrf

            <div class="mb-3">
                <label for="name" class="form-label">Name</label>
                <input type="text" name="name" id="name" class="form-control" value="{{ old('name') }}" required>
            </div>

            <div class="mb-3">
                <label for="email" class="form-label">Email</label>
                <input type="email" name="email" id="email" class="form-control" value="{{ old('email') }}" required>
            </div>

            <div class="mb-3">
                <label for="password" class="form-label">Password</label>
                <input type="password" name="password" id="password" class="form-control" required>
            </div>

            <div class="mb-3">
                <div class="g-recaptcha" data-sitekey="{{ env('GOOGLE_RECAPTCHA_SITE_KEY') }}"></div>
            </div>

            <button type="submit" class="btn btn-primary w-100">Register</button>
        </form>
    </div>
</div>

</body>
</html>

How to integrate Google recaptcha in Laravel 12

📌 Step 4: Validate reCAPTCHA and store User in Controller (UserController.php)

    public function registerIndex()
    {
        return view('register');
    }

    public function registerUser(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|string|min:6',
            'g-recaptcha-response' => 'required',
        ]);

        // Validate Google reCAPTCHA
        $response = Http::asForm()->post('https://www.google.com/recaptcha/api/siteverify', [
            'secret' => env('GOOGLE_RECAPTCHA_SECRET_KEY'),
            'response' => $request->input('g-recaptcha-response'),
            'remoteip' => $request->ip(),
        ]);

        if (! ($response->json()['success'] ?? false)) {
            return back()->withErrors(['captcha' => 'Google reCAPTCHA validation failed.'])->withInput();
        }

        // Store user
        User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        return redirect()->route('register.index')->with('success', 'User registered successfully!');
    }

🎉 That's It!

You’ve now added Google reCAPTCHA to your Laravel 12 project! 💪

Your forms are now safer from bots and spam. Always protect your user-facing features with simple but strong layers of defense like reCAPTCHA. 🔐

Thank you for reading this article 😊

For any query do not hesitate to comment 💬


Previous Post Next Post

Contact Form