Laravel 9 Generate Captcha Code using Bootdetect Package Example

Jul 05, 2022 . Admin


Hi Dev,

In this post, we will learn Laravel 9 Generate Captcha Code using Bootdetect Package Example. we will help you to give example of How to Use Captcha in Laravel 9 Forms for Validation. We will look at example of Laravel 9 Word Captcha Code and Validation Example. I would like to show you Laravel 9 Captcha Tutorial. Follow bellow tutorial step of How to Generate Captcha Code in Laravel 9.

if you want to see example of Laravel 9 Captcha Tutorial then you are a right place.

This article will give you simple example of How to Add Captcha code validate and refresh captcha in Laravel 9 Form?

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip the following step.

composer create-project laravel/laravel example-app
Step 2: Install Captcha package

In first step we will install captcha-com/laravel-captcha package for generate captcha code image. this package through we can generate captcha code image for our project. so first fire bellow command in your cmd or terminal:

composer require captcha-com/laravel-captcha:"4.*"

Now we need to add provider path and alias path in config/app.php file so open that file and add bellow code.

config/app.php
return [
    $provides => [
        LaravelCaptcha\Providers\LaravelCaptchaServiceProvider::class
    ],

Now we will run bellow command that way it will generate app/captcha.php file for configration and we can change and customize easily.

php artisan vendor:publish
Step 3: Create Controller

We will use Laravel default auth mechanism to complete our project. So we have no need to create new controller. In your register controller your default validator function will be look like this.

app/Http/Controllers/Auth/RegisterController.php
protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed'
        ]);
    }

Now you have to just add one line code. see the below code

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed',
            'CaptchaCode' => 'required|valid_captcha'
        ]);
    }

Look we just added 'CaptchaCode' => 'required|valid_captcha' line to get Capctcha code image.

Step 4: Create Blade File

This is the last step. So go to your resources/views/auth/register.blade.php and add the following code to your register form.

resources/views/auth/register.blade.php
    <div class="form-group{{ $errors->has('CaptchaCode') ? ' has-error' : '' }}">
        <label class="col-md-4 control-label">Captcha</label>

        <div class="col-md-6">
            {!! captcha_image_html('ContactCaptcha') !!}
        <input class="form-control" type="text" id="CaptchaCode" name="CaptchaCode">

            @if ($errors->has('CaptchaCode'))
                <span class="help-block">
                    <strong>{{ $errors->first('CaptchaCode') }}</strong>
                </span>
            @endif
        </div>
    </div>

It will help you...

#Laravel 9