Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 8 Google Recaptcha Form Validation Example Tutorial

Laravel 8 google recaptcha form validation example tutorial

Posted on October 10, 2020September 26, 2021 By XpertPhp

In this tutorial, we are going to integrate the Google reCAPTCHA in Laravel 8. normally captcha is used for security purposes and only human users can pass through. computers or bots are not solving a captcha. so we will learn about captcha that how to integrate the Google reCAPTCHA in laravel 8.

In this example, we will integrate the Google reCAPTCHA in laravel 8 using the “anhskohbo/no-captcha” pacakge. so you can follow the below step.

Overview
Step 1: Install Laravel
Step 2: Install Package
Step 3: Add providers and aliases
Step 4: Get and Set Google Captcha Secrets
Step 5: Create Route
Step 6: Create a Controller
Step 7: Create Blade Files
Step 8: Run The Application

Step 1: Install Laravel
We are going to install laravel 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command.

1
composer create-project --prefer-dist laravel/laravel laravel8_google_captcha

Step 2: Install Package
Now, we are going to install the “anhskohbo/no-captcha” package using the below command.

1
composer require anhskohbo/no-captcha

Step 3: Add providers and aliases
We will add below providers and aliases in the “config/app.php” file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'providers' => [
....
Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class
],
'aliases' => [
....
'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
]

Step 4: Get and Set Google Captcha Secrets
First, we need to Google reCAPTCHA site key and secret key. so first we go to Google reCAPTCHA panel and get the site key and secret key.
After getting the keys, we will set the key in the .env file. so you can see our code.

1
2
NOCAPTCHA_SITEKEY=Enter_Site_Key
NOCAPTCHA_SECRET=Enter_Secret_Key

Step 5: Create Route
Add the following route code in the “routes/web.php” file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
use App\Http\Controllers\StudentController;
 
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
 
 
Route::get('student',[StudentController::class, 'index']);
Route::post('student',[StudentController::class, 'save'])->name('student.save');
?>

Step 6: Create a Controller
Here below command help to create the controller.

1
php artisan make:controller StudentController

StudentController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator,Redirect,Response,File;
 
class StudentController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('student');
    }
 
 
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function save(Request $request)
    {
request()->validate([
        'txtFirstName' => 'required',
        'txtLastName' => 'required',
        'txtEmail' => 'required',
        'txtMobile' => 'required',
        'g-recaptcha-response' => 'required|captcha',
        ]);
        dd('captcha code is valid');
}
}
?>

Step 7: Create Blade Files
So finally, first we will create the student.blade.php files in the “resources/views” directory.
student.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Laravel 8 Google Recaptcha Form Validation Example Tutorial - XpertPhp</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
 
<div class="container">
  <form method="post" name="frmAdd" id="frmAdd" action="{{ route('student.save') }}">
  @csrf
  <div class="form-group">
    <label for="txtFname">First Name:</label>
    <input type="text" name="txtFname" class="form-control" id="txtFname" value="">
@if ($errors->has('txtFname'))
<span class="error">
<p>{{ $errors->first('txtFname') }}</p>
</span>
@endif
  </div>
  <div class="form-group">
    <label for="txtLname">Last Name:</label>
    <input type="text" name="txtLname" class="form-control" id="txtLname" value="">
@if ($errors->has('txtLname'))
<span class="error">
<p>{{ $errors->first('txtLname') }}</p>
</span>
@endif
  </div>
  <div class="form-group">
    <label for="txtEmail">Email:</label>
    <input type="text" name="txtEmail" class="form-control" id="txtEmail" value="">
@if ($errors->has('txtEmail'))
<span class="error">
<p>{{ $errors->first('txtEmail') }}</p>
</span>
@endif
  </div>
  <div class="form-group">
    <label for="txtMobile">Mobile:</label>
    <input type="text" name="txtMobile" class="form-control" id="txtMobile" value="">
@if ($errors->has('txtMobile'))
<span class="error">
<p>{{ $errors->first('txtMobile') }}</p>
</span>
@endif
  </div>
  
   <div class="form-group>
<label class="control-label">Captcha</label>
{!! app('captcha')->display() !!}
@if ($errors->has('g-recaptcha-response'))
<span class="error">
<p>{{ $errors->first('g-recaptcha-response') }}</p>
</span>
@endif
</div>
  <button type="submit" class="btn btn-default">Submit</button>        
</form>
</div>
</body>
</html>

Step 8: Run The Application
We can start the server and run this application using the below command.

1
php artisan serve

Now we will run our example using the below Url in the browser.

1
http://127.0.0.1:8000/student

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Laravel 7 Fullcalendar Ajax Example Tutorial
  • Laravel 6 Stripe Payment Gateway Integrate Example
  • Laravel 7 Socialite Facebook Login Tutorial Example
  • laravel 8 target class controller does not exist
  • laravel eloquent WhereNotNull query example
Laravel Tags:Google reCAPTCHA in PHP, Laravel 8, laravel 8 tutorial

Post navigation

Previous Post: Angular 10 – Can’t bind to ‘formGroup’ since it isn’t a known property of ‘form’
Next Post: Angular 10 Multiple Image Upload Example Tutorial

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com