DEV Community

Cover image for Add Captcha On Laravel Forms
Arman Rahman
Arman Rahman

Posted on

7

Add Captcha On Laravel Forms

To add Captcha to Laravel form without any API Connection

Image description

Install mews/captcha



composer require mews/captcha


Enter fullscreen mode Exit fullscreen mode

Find the providers key in config/app.php and register the Captcha Service Provider. for for Laravel 5.1+



'providers' => [
        // ...
        Mews\Captcha\CaptchaServiceProvider::class,
    ]


Enter fullscreen mode Exit fullscreen mode

Find the aliases key in config/app.php.



'aliases' => [
        // ...
        'Captcha' => Mews\Captcha\Facades\Captcha::class,
    ]


Enter fullscreen mode Exit fullscreen mode

publish mews/captcha by running -



$ php artisan vendor:publish


Enter fullscreen mode Exit fullscreen mode

Add this to .env to disable captcha



CAPTCHA_DISABLE=true


Enter fullscreen mode Exit fullscreen mode

Add this on laravel blade



 //register.blade.php
    <img src="{{ captcha_src() }}" alt="captcha">
        <div class="mt-2"></div>
        <input 
            type="text" name="captcha" class="form-control @error('captcha') is-invalid @enderror" placeholder="Please Insert Captch"
            >
         @error('captcha') 
         <div class="invalid-feedback">{{ $message }}</div> @enderror 


Enter fullscreen mode Exit fullscreen mode

in controller validate like this -



$request->validate([
'captcha' => 'required|captcha'
],[],[
     'captcha.required' => 'Captcha is required',
     'captcha.captcha' => 'Captcha is invalid',
])


Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs