Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ifrit's avatar

Getting reCAPTCHA to work

I'm trying to get the google reCAPTCHA to work and I'm not sure if I'm doing something wrong. I've added my site key and secret key in my .env file

RECAPTCHA_SITE_KEY=site_key
RECAPTCHA_SECRET_KEY=secret_key

and then I added this to my master.blade.php template

<script src='https://www.google.com/recaptcha/api.js'></script>s

and I added this to my form

<div class="g-recaptcha" data-sitekey="{!! env('RECAPTCHA_SITE_KEY') !!}"></div>

and when I refresh my browser the reCAPTCHA doesn't show up

0 likes
15 replies
Snapey's avatar

Any browser console errors? (a different js error could stop all js)

dumb question, but the view you are showing does extend master?

Ifrit's avatar

@Snapey - Yes it does extend the master. and I have this error in my console

Error: Missing required parameters in RecaptchaOptions: sitekey

Which I think means that it isn't getting the value in the .env file

Snapey's avatar

View source in the browser and check what is output?

Ifrit's avatar

@Snapey - When I check my view source I only get this

<div class="g-recaptcha" data-sitekey=""></div>

@splintter - I tried that one but I'm getting

Class captcha does not exist
Snapey's avatar

To check if you are interacting with the .env file can you try this;

<div class="g-recaptcha" data-sitekey="{!! env('RECAPTCHA_SITE_KEY','NO-KEY-FOUND') !!}"></div>

and then report the view source output again

splintter's avatar

This is how I got nocaptcha working fine with my L5.2 install:

composer require anhskohbo/no-captcha

Add to your config/app.php the following line into your providers array:

Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class,

Also, add to your aliases:

'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,

Add to your view:

{!! app('captcha')->display(); !!}

Do not forget to include into your env:

NOCAPTCHA_SITEKEY=
NOCAPTCHA_SECRET=
albusSeverus's avatar

Hi, Im stuck at the same point as the person trying to ask this question. Was working fine and then my hdd crashed and reinstalled everything. After bringing everything back to life on local, captcha stopped working. Tried deleting the .env file and recreating, cleared the cache and other parameters. Still not able to figure out why? The app runs fine but if I tried to dd() any env variables, it returns null. In short, the .env file is not read. Please suggest me a solution for this issue.

SmoKa's avatar

I had the same problem, I solved it by doing a

php artisan config:clear

Hope it help

4 likes
DaleL's avatar

Sorry if i'm late to the party here but i'm facing the exact same issue. If you want to keep the config cached, go to bootstrap/cache/config.php and look for:

'captcha' => array ( 'secret' => , 'sitekey' => ,

Chances are if your facing the same issue as me, these will show as NULL. For whatever reason, the correct values aren't being cached by Laravel from the .env file. Simply update these values with your actual sitekey and secret and wrap them in ' ' like the rest of the values, save the file and retry, issue should now be resolved. You'll have to do this each time you clear and cache the config.

Snapey's avatar

@DaleL never edit anything in that folder.

Just run artisan config:clear

DaleL's avatar

@Snapey Sorry if i sound arrogant but why not? If there's an issue with the .env entry being cached what's the harm in manually updating the cached file to the correct values? Appreciate the response, still learning Laravel.

Please or to participate in this conversation.