1、composer安装必要的包
composer require mews/captcha
2、配置: 在 config/app.php 中 找到 aliases数组加入以下代码
'providers' => [ ... Mews\Captcha\CaptchaServiceProvider::class, ],
'aliases' => [ ... 'Captcha' => Mews\Captcha\Facades\Captcha::class, ],
3、运行以下代码发布,选择“Provider: Mews\Captcha\CaptchaServiceProvider”的序号, 生成配置文件 config/captcha.php
php artisan vendor:publish
4、在config/captcha.php文件中进行必要的相关配置(可保持默认)
5、前端通过以下代码直接调用
<img src="{{captcha_src('math')}}" onclick="this.src='{{captcha_src('math')}}?'+Math.random()">
6、后端验证
return Validator::make($data, [ ...... 'captcha' => ['required','captcha'], ],[ ...... 'captcha.required'=>'Please input captcha', 'captcha.captcha'=>'captcha was wrong, please input again' ]);