Captcha Code and Validation Example using Botdetect Package in Laravel

How to Generate Captcha Code in Laravel is the topic today to discuss. Captcha stands for Completely Automated Public Turing test. It is mainly used as a security test to ensure only human users can pass through. Computers or bots are not able of solving a captcha.

You can see that many web application uses captcha code to protect their form data before saving database. Sometime many users uses math for captcha or someone uses image captcha. 

In this tutorial i will show you how we can integrate captcha code in our laravel web application. To do that i will use bootdetect package to create captcha code and validation.

There are different types of captcha we can use some protection. The logic behind why websites implement CAPTCHA codes into their registration processes is because of spam. In this post i show you very simple and from scratch of generate captcha code image for your laravel app. 

captcha-code-and-validation-example-in-laravel

Step 1: Configure Laravel Project

Install the new Laravel Project by the running following command.

composer create-project --prefer-dist laravel/laravel laravelcaptcha

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

Finally, Our How to Generate Captcha Code in Laravel is over. Hope it will work for you. 

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website