A simple, Laravel-native package for integrating Google reCAPTCHA v2 and v3 with both standard forms and Livewire components.
composer require snipify-dev/laravel-captcha
Add your reCAPTCHA keys to your .env file:
# Choose your default version
RECAPTCHA_VERSION=v3
# reCAPTCHA v3 Keys (recommended)
RECAPTCHAV3_SECRET=your_v3_secret_key_here
RECAPTCHAV3_SITEKEY=your_v3_site_key_here
# reCAPTCHA v2 Keys (optional)
RECAPTCHAV2_SECRET=your_v2_secret_key_here
RECAPTCHAV2_SITEKEY=your_v2_site_key_here
php artisan config:clear
That's it! The package is ready to use.
<?php
namespace App\Livewire;
use Livewire\Component;
use SnipifyDev\LaravelCaptcha\Rules\RecaptchaValidationRule;
class ContactForm extends Component
{
public $name = '';
public $email = '';
public $message = '';
public $captchaToken = '';
public function submit()
{
$this->validate([
'name' => 'required|min:3',
'email' => 'required|email',
'message' => 'required|min:10',
'captchaToken' => ['required', new RecaptchaValidationRule('contact')],
]);
// Process your form here...
$this->reset();
session()->flash('success', 'Message sent successfully!');
}
public function render()
{
return view('livewire.contact-form');
}
}
<div>
<form wire:submit.prevent="submit" class="space-y-4">
<div>
<label>Name</label>
<input type="text" wire:model="name">
@error('name') <span class="text-red-500">{{ $message }}</span> @enderror
</div>
<div>
<label>Email</label>
<input type="email" wire:model="email">
@error('email') <span class="text-red-500">{{ $message }}</span> @enderror
</div>
<div>
<label>Message</label>
<textarea wire:model="message"></textarea>
@error('message') <span class="text-red-500">{{ $message }}</span> @enderror
</div>
{{-- Captcha field --}}
<x-captcha-livewire-field wire-model="captchaToken" action="contact" />
@error('captchaToken') <span class="text-red-500">{{ $message }}</span> @enderror
<button type="submit">Submit Form</button>
</form>
</div>
<?php
use Illuminate\Http\Request;
use SnipifyDev\LaravelCaptcha\Rules\RecaptchaValidationRule;
class ContactController extends Controller
{
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email',
'message' => 'required|string|min:10',
'g-recaptcha-response' => ['required', new RecaptchaValidationRule('contact')],
]);
// Process form...
return back()->with('success', 'Message sent!');
}
}
<x-captcha-livewire-field
wire-model="captchaToken" {{-- Required: Livewire property --}}
action="contact" {{-- Optional: Action for v3 scoring --}}
version="v3" {{-- Optional: Force version (v2/v3) --}}
/>
Add this to your layout before closing </body> tag:
<x-captcha-script />
// Basic usage with any action name
new RecaptchaValidationRule('login')
new RecaptchaValidationRule('contact')
new RecaptchaValidationRule('signup')
new RecaptchaValidationRule('any_action_name')
// Force specific version if needed
new RecaptchaValidationRule('login', null, 'v2') // Force v2
new RecaptchaValidationRule('login', null, 'v3') // Force v3
The package automatically disables validation in testing environments:
public function test_contact_form_submission()
{
$response = $this->post('/contact', [
'name' => 'John Doe',
'email' => 'john@example.com',
'message' => 'Test message',
'captchaToken' => 'fake-token', // Ignored in testing
]);
$response->assertRedirect()->assertSessionHas('success');
}
"Site key not found" Error
php artisan config:clear
# Verify your .env keys are correct
Validation Always Fails
.envRECAPTCHA_FAKE_DEVELOPMENT=trueJavaScript Errors
<x-captcha-script /> before </body>Livewire Integration
public $captchaToken = '';wire-model="captchaToken" in the componentOptionally publish the config file for advanced customization:
php artisan vendor:publish --tag=recaptcha-config
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?