How to Create Captcha Code using Bootdetect Package in Laravel 9?

10-Apr-2023

.

Admin

How to Create Captcha Code using Bootdetect Package in Laravel 9?

Hi Dev,

This example is focused on How to Use Captcha in Laravel 9 Forms for Validation. I would like to show you Laravel 9 Word Captcha Code and Validation Example. We will look at example of Laravel 9 Captcha Tutorial. I explained simply step by step How to Generate Captcha Code in Laravel 9.

Here, Creating a basic example of Laravel 9 Generate Capt

Hi Dev,

In this example, I will show you How to Create Captcha Code using Bootdetect Package in Laravel 9 . I explained simply step by step Laravel 9 Word Captcha Code and Validation Example. In this article, we will implement a Laravel 9 Captcha Tutorial. let’s discuss about How to Generate Captcha Code in Laravel 9.

So, let's follow few step to create example of Laravel 9 Generate Captcha Code using Bootdetect Package Example.

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: Add 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: Add 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