Latest Code Tutorials

Laravel Google reCaptcha Tutorial


Laravel Google reCaptcha Tutorial is the topic, we will discuss today. The reCaptcha is an open service that defends your site from spam and abuse. It uses advanced risk analysis techniques to show humans and bots apart. Using reCaptcha, you must sign up for the API key pair for your website. The key pair consists of the site key and secret key. In this example, we can use anhskohbo/no-captcha package for creating reCaptcha.

Laravel Google reCaptcha Tutorial

First, we install Laravel Application.

Step 1: Configure Laravel Application

Install the brand new Laravel Application by the typing following command.

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

Step 2: Install anhskohbo/no-captcha Package

First, install anhskohbo/no-captcha Package for Google reCaptcha code. Using this Package, we can generate reCaptcha code for our Application. Switch to your command line and hit below command.

composer require anhskohbo/no-captcha

Step 3: Publish Config File

This package supports the auto-discovery feature of Laravel 5.5+.

Type below command to Publish Config File

php artisan vendor:publish --provider="Anhskohbo\NoCaptcha\NoCaptchaServiceProvider"

Step 4: Set Google Site Key and Secret Key

Next, we need to set Google Site Key and Secret Key in .env file.  If you don’t have Site Key and Secret Key, then you have produce from below link.

Generate Recaptcha

reCaptcha Tutorial in Laravel

Now open the .env file and add this two keys.


Step 4: Create one controller

php artisan make:controller RecaptchaController --resource

It will create one controller file called RecaptchaController.php file.

Define validation in controller file.


public function create()
        return view('recaptchacreate');
public function store(Request $request)
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required|min:6',
            'g-recaptcha-response' => 'required|captcha'

        return "success";

In store() function, we have put the validation. If any validation fails it throws an error.

Step 5: Define Routes 

We list route in routes  >>  web.php file. So let us do it.


Route::get('recaptchacreate', 'RecaptchaController@create');
Route::post('store', 'RecaptchaController@store');

In the web.php file, we define Two(2) routes the first route for display the login page, the second route for post request.

Step 6: Create one view file

Create a file in the resources  >>  views  >>   recaptchacreate.blade.php and set the following code in it.


<html lang="en">
    <title>reCAPTCHA Code in Laravel</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <link href="" rel="stylesheet">   
    <script src=""></script>  
    <script src=""></script>  
<div class="container">
      <h2>reCAPTCHA Code in Laravel</h2><br/>
      @if ($errors->any())
      <div class="alert alert-danger">
              @foreach ($errors->all() as $error)
                  <li>{{ $error }}</li>
      </div><br />
      <form method="post" action="{{url('store')}}">
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <label for="Name">Name:</label>
            <input type="text" class="form-control" name="name">
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="Email">Email:</label>
              <input type="text" class="form-control" name="email">
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-4">
              <label for="Password">Password:</label>
              <input type="password" class="form-control" name="password">
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
          <label for="ReCaptcha">Recaptcha:</label>
          {!! NoCaptcha::renderJs() !!}
          {!! NoCaptcha::display() !!}
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <button type="submit" class="btn btn-success">Submit</button>


Google reCaptcha Tutorial

Next, Start Laravel Development server by hit following command.

php artisan serve

If we do not enter any fields or enter invalid reCaptcha code, then we will get the errors.

Laravel 5.6 Google reCaptcha Tutorial

Finally, Our Laravel Google reCaptcha Tutorial is over. Thank You.

  1. Jitesh Lakhwani says

    There is small change in your RecaptchaController.php in store method you need to change the way you are using validator else it will trow an ssl error and there is also one extra tag at the last in the blade file.

    please use the below code for the store method:
    $validator = Validator::make($request->all(), [
    ‘name’ => ‘required’,
    ’email’ => ‘required|email’,
    ‘password’ => ‘required|min:6’,
    ‘g-recaptcha-response’ => ‘required|captcha’

    i Googled a lot and invested my 45 min. then after changing this it started working properly.


  2. shamim says

    Why Class ‘NoCaptcha’ not found sir in recaptchacreate page

  3. Ivan says

    There is an error :
    “Class ‘NoCaptcha’ not found”

  4. Rizki says

    can you make this tutorial to laravel 5.7 sir? i its not work when i try in my laravel 5.7 project. when i click checkbox “im not robot” chaptcha not display the puzzle. when i check console log its say failed load the recaptcha js, even though i have put {!! NoCaptcha::renderJs() !!} and {!! NoCaptcha::display() !!}on my form

  5. Yasmine says

    images not showing only i’m not robot

  6. vivek says

    Argument 1 passed to GuzzleHttp\Client::__construct() must be of the type array, null given, called in D:\autocleanse\vendor\anhskohbo\no-captcha\src\NoCaptcha.php on line 50 (View: D:\autocleanse\resources\views\welcome.blade.php)

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.