当前位置:网站首页>Laravel 验证唯一时排除修改时的数据
Laravel 验证唯一时排除修改时的数据
2022-08-02 03:26:00 【陌潇】
laravel验证唯一性很简单:unique:user,phone 这个就是验证user表的phone字段是否唯一,但是修改数据时这样就验证过不了,怎么办呢?
use Illuminate\Support\Facades\Validator;
$msg = [
'name.required' => '姓名不能为空',
'phone.required' => '手机不能为空',
'phone.regex' => '请输入正确的手机号',
'phone.unique' => '手机号已经存在',
];
$rules = [
'name' => 'required',
//此处注意,phone后面一定要加逗号(,)不然他会把phone和id拼接,从而找不到字段报错
'phone' => 'required|regex:/^1[345789][0-9]{9}$/|unique:user,phone,'.$id,
];
$validator = Validator::make($receive, $rules, $messages);
if($validator->fails()){
$error = '';
foreach ($validator->errors()->getMessages() as $v) {
$error = $v[0];
break;
}
return sendError($error, 422);
}
这样一个验证就可以了。
边栏推荐
- Alfa: 1 vulnhub walkthrough
- hackmyvm-random walkthrough
- Pycharm packages the project as an exe file
- 利用cookie获取admin权限 CTF基础题
- pytorch:保存和加载模型
- 英语每日打卡
- 管理会计(对内)指引、管理会计要素及其具体内容(可能考,考前记一下,推荐记一下四个大点即可)、
- CSRF(跨站请求伪造)
- 会计凭证概述、原始凭证、原始凭证的种类、原始凭证的基本内容、原始凭证的填制要求、原始凭证的审核
- (1) introduction to Thinkphp6, installation view, template rendering, variable assignment
猜你喜欢

记账凭证的种类、记账凭证的基本内容、记账凭证的填制要求、记账凭证的审核

mysql 原生语句点滴学习记录

会计账簿、会计账簿概述、会计账簿的启用与登记要求、会计账簿的格式和登记方法

hackmyvm: juggling walkthrough

Introduction to PHP (self-study notes)

laravel 查询数据库获取结果如何判断是否为空?

CTF entry md5

How to calculate the distance between two points on the earth (with formula derivation)

Command Execution Vulnerability

Several interesting ways to open PHP: from basic to perverted
随机推荐
How to log in to Alibaba Cloud server using the admin account
文件包含漏洞
PHP实现搜索框的自动反查提示
hackmyvm: again walkthrough
CTF入门笔记之SQL注入
Praying: 1 vulnhub walkthrough
(2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
Eric target penetration test complete tutorial
什么是广告电商商业模式?这几个门派告诉你
Phonebook
[Hello World教程] 使用HBuilder和Uni-app 生成一个简单的微信小程序DEMO
file contains vulnerabilities
会计凭证概述、原始凭证、原始凭证的种类、原始凭证的基本内容、原始凭证的填制要求、原始凭证的审核
CTF入门之md5
hackmyvm: controller walkthrough
Several interesting ways to open PHP: from basic to perverted
对账、结账、错账更正方法、划线更正法、红字更正法、补充登记法
重点考:从债劵的角度来看交易性金融资产
真·杂项:资本论阅读笔记(随缘更新)
机器学习1